Wait() isn't waiting inside functions

Hey, i was trying to add a wait() inside a function, before it does the other stuff, And it just completely ignored the wait() and prints. i also tried using task.wait()

local function updateCamera(playerSubject)
	playerSubject.Character:WaitForChild("Humanoid").Died:Connect(function()
		wait(4)
		print("spectating player died")
	end)
	
end
1 Like

Did you call the function? Can you show the full code?

Also change: wait(4) to task.wait(4). It’s faster.

1 Like

yes i did call the function, everything works, its just that the wait is ignored.

this is the full code

local function updateCamera(playerSubject)
	playerSubject.Character:WaitForChild("Humanoid").Died:Connect(function()
	    wait(4)
		print("spectating player died")
	end)
	
	pcall(function() 
		spectateFrame.Nametext.Text = tostring(playerSubject)
		cam.CameraSubject = playerSubject.Character
	end)
	
end

That’s because that’s an event listener. Once the Humanoid.Died gets fired, the function gets called since there is a wait for 4 seconds. It would wait and then print “spectating player died”. Unless the print() function is called in an instant. Since there is a wait, that’s unlikely. You could try this to see if the wait is actually working.

local start = os.clock()
task.wait(4)
print(start - os.clock())
print("spectating player died")
1 Like

Also wait is not safe to use there would be a time where this wait 4 seconds could actually be greater than 4 seconds. task.wait is safer to use since this is being handled by the task scheduler.

1 Like

Thanks, it worked. i changed it to task.wait

1 Like