Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Function is causing a delay in my script; what is a solution?

Asked by 5 years ago
Edited 5 years ago

This script is pretty long, so I'll give you the highlights.

local makeCameraNormal = function(client)
    -- Make their camera normal again
    local localScriptClone = game.ServerStorage.scripts.Local.changeCameraNormal:Clone()
    localScriptClone.Parent = client.PlayerGui
    wait(4)
    localScriptClone:Destroy()
end

thisFolder.spawnPlayer.OnServerEvent:Connect(function(client)
    -- Make player's Camera normal
    makeCameraNormal(client)

    -- Lastly, give a ForceField for 3 seconds.
    if not client.Character:FindFirstChildWhichIsA("ForceField") then
        local ff = Instance.new("ForceField")
        ff.Name = "ff"
        ff.Parent = client.Character
        wait(5)
        ff:Destroy()
    end
end)

"makeCameraNormal" used to not be a function; its code would replace the code in lines 10-11. However, it would delay lines 14-20. I made the "makeCameraNormal" function to stop the delaying in the script so it would do the function along with lines 14-20 at the same time. However, the function is not fulfilling its desired purpose. Please let me know if you have a solution as to making these run at the same time.

1 answer

Log in to vote
1
Answered by
mc3334 649 Moderation Voter
5 years ago

Heres your solution. Simply spawn the function.

local makeCameraNormal = function(client)
    -- Make their camera normal again
    local localScriptClone = game.ServerStorage.scripts.Local.changeCameraNormal:Clone()
    localScriptClone.Parent = client.PlayerGui
    wait(4)
    localScriptClone:Destroy()
end

thisFolder.spawnPlayer.OnServerEvent:Connect(function(client)
    -- Make player's Camera normal
    spawn(function()
         makeCameraNormal(client)
    end)

    -- Lastly, give a ForceField for 3 seconds.
    if not client.Character:FindFirstChildWhichIsA("ForceField") then
        local ff = Instance.new("ForceField")
        ff.Name = "ff"
        ff.Parent = client.Character
        wait(5)
        ff:Destroy()
    end
end)
Ad

Answer this question