Does task.wait(x) ignore the second player firing the game.Players.PlayerRemoving event?

I have the following code in my “PlotHandler” server-script in ServerScriptService:

game:GetService("Players").PlayerRemoving:Connect(function(player)

	local PlotNumberVariable = player:WaitForChild("PlotNumber").Value

	local PlotsFolder = game.Workspace:WaitForChild("Plots")
	local PlayersPlot = PlotsFolder:WaitForChild(PlotNumberVariable)



	warn("Removing "..player.Name.."'s Plot.")
	
	PlayersPlot:WaitForChild("Owner").Value = "" -- Reset plot owner value
	
	task.wait(5) -- Give the DataStore script some time to save the plot
    
    local ElementsPlacedFolder = PlayersPlot:FindFirstChild("ElementsPlaced")
    ElementsPlacedFolder:ClearAllChildren() -- Remove all items in the plot

end)

What I’m wondering is if a player leaves, the task.wait(5) is there so it waits until the DataStore has collected the items on their plot and saved it. If another player leaves within those 5 seconds, will the PlayerRemoving event be ignored because it is already yielding?

I can probably avoid this with doing a coroutine but I have done that in the past and it only ran once and never again, so if possible, can anyone possibly help me with that?

1 Like

Functions connected to events are executed in their own thread of execution, so no. You can verify this via the following script.

local part = script.Parent

local function onTouched(touchedPart)
    task.wait(5) --this won't delay future executions of the function
    print("Hello world!")
end

part.Touched:Connect(onTouched)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.