Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.