0

我正在使用来自 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)
4

1 回答 1

0

在不知道您的资源管理器中有哪些脚本的情况下,您的问题可能出在您的 onPlayerEntered 函数中。您正在迭代 5 个未命名的、可能是随机的脚本并将它们插入到播放器中。

根据https://developer.roblox.com/api-reference/function/Instance/GetChildren,元素script:GetChildren()的顺序取决于其 Parent 属性的设置顺序。按名称显式克隆子项或遍历 Scripts 数组的长度可能更安全。

要对此进行调试,我可能建议将您的 for 循环修改为:

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-20T20:41:09.570 回答