Roblox脚本不再存在于游戏中,但出现在新创建的角色中

我在使用从Roblox市场中下载的脚本并对其进行了大幅修改。原始版本已不再存在于游戏中,但出现在新角色的子级中,尽管我没有脚本可以将其放置在那里。为什么会这样发生,我该如何修复?我在任何其他地方都没有发现类似的情况。

以下是原始脚本:

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false

function getTool()
    for _, kid in ipairs(script.Parent:GetChildren()) do
        if kid.className == "Tool" then return kid end
    end
    return nil
end

mouse.KeyDown:connect(function (key) -- Run function
    key = string.lower(key)
    if string.byte(key) == 48 then
        running = true
        local keyConnection = mouse.KeyUp:connect(function (key)
            if string.byte(key) == 48 then
                running = false
            end
        end)
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
            wait()
        end
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 85
        repeat wait () until running == false
        keyConnection:disconnect()
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
            wait()
        end
    end
end)

以下是修改后的脚本:

wait(1)
local Player = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local running = false
local startSpeed = 10
local Speed = script.Speed
Speed.Value = 10
local Earnings = script.Earnings
Earnings.Value = 25
local Cash = script.Cash
Cash.Value = 10000
local speedUpCost = script.speedUpCost
speedUpCost.Value = 100
local earnUpCost = script.earnUpCost
earnUpCost.Value = 100

game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = Speed.Value

function getTool()
    for _, kid in ipairs(script.Parent:GetChildren()) do
        if kid.className == "Tool" then return kid end
    end
    return nil
end

mouse.KeyDown:connect(function (key)
    key = string.lower(key)
    if string.byte(key) == 48 then
        running = true
        local keyConnection = mouse.KeyUp:connect(function (key)
            if string.byte(key) == 48 then
                running = false
            end
        end)
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = Speed.Value
        repeat wait () until running == false
        keyConnection:disconnect()
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = startSpeed
    end
end)

以下是我使用的代码,将修改后的脚本和其他一些脚本作为新角色的子级:(这些其他脚本不包括我修改的原始脚本)

function onPlayerEntered(player)
    player.CharacterAdded:connect(function (char)
        local Scripts = script:GetChildren()
        for i=1,5 do
            local s = Scripts[i]:clone()
            s.Parent = char
            s.Disabled = false
        end
    end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)
点赞
用户2860267
用户2860267

不知道你的 explorer 中有哪些脚本,但看起来你的问题可能出在 onPlayerEntered 函数中。你正在遍历 5 个未命名、可能是随机的脚本,并将它们插入到玩家中。

根据 https://developer.roblox.com/api-reference/function/Instance/GetChildren 中的说明,script:GetChildren() 中元素的顺序取决于它们 Parent 属性的设置顺序。最好通过名称显式克隆子元素,或者遍历脚本数组的长度。

为了调试该问题,我建议修改你的循环:

local Scripts = script:GetChildren()
for i=1,#Scripts,1 do
    local s = Scripts[i]:clone()
    s.Parent = char
    s.Disabled = false
    print("Inserting script : ", s.Name, " - ", s:GetFullName())
end

这应该可以告诉你哪些脚本实际上被添加了。

或者,你可以将你想要添加的脚本添加到 StarterPlayer > StarterPlayerScripts 文件夹中,当玩家加入游戏时,它们将自动添加到玩家中。

2018-12-20 20:41:09