Roblox脚本在游戏中的任何地方都不再存在,显示为新创建角色的子代

时间:2018-12-09 03:44:23

标签: lua 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)

1 个答案:

答案 0 :(得分:0)

在不知道资源管理器中包含什么脚本的情况下,看来您的问题可能出在onPlayerEntered函数中。您要遍历5个未命名的脚本(可能是随机的)并将其插入Player。

根据https://developer.roblox.com/api-reference/function/Instance/GetChildrenscript: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文件夹中,并且当播放器加入时,它们会自动添加到播放器中。