Task.wait() not working

Hello,

So I’m trying to fix a camera’s position while a remote event is being processed and completed. But the task.wait(1) after the firing of the event doesn’t work. There is a print statement after the task.wait() but it does not print at all. When I remove the task.wait(), then it prints the value.

	cam.CameraType = Enum.CameraType.Fixed
	game.ReplicatedStorage.CompleteMorphEvent:FireServer()
	task.wait(1) -- Below code does not run after this
	cam.CameraType = Enum.CameraType.Custom
	print('Set!') -- Does not print 'Set' but when I remove the wait() it does.

All help is appreciated, thanks.

EDIT: It’s in a local script, in a button click event.

Have you tried using wait()? Maybe that would work.

I tried that too, but it didn’t work.

I tried it with my own remote and it worked, im not sure whats wrong with your code.

Wait, it worked for you? The code is in a Button.MouseButton1Down event and it set the camera type to “Fixed” but then it stayed fixed, it does not set it back to custom.

Its probably a problem within your remote because I tried it with a normal remote event (which does nothing) and after a second it printed out set and set my camera to custom. Can you show your code that fires when the remote is called?

The remote event doesn’t play a role in this code though. It’s just fired, but what I’m looking for is the code after the remote event being fired executed. It’s not taking place on a remote event but when a button (GUI) is clicked.

    cam.CameraType = Enum.CameraType.Fixed
    task.spawn(function()
	  game.ReplicatedStorage.CompleteMorphEvent:FireServer()
    end)
	task.wait(1) -- Below code does not run after this
	cam.CameraType = Enum.CameraType.Custom
	print('Set!') -- Does not print 'Set' but when I remove the wait() it does.

can you try this?

1 Like

I assume the remote disables/destroys a UI(or a script) that has direct influence to your UI code? Therefore waiting for it to complete disables the script before the print runs?

Maybe try changing the MouseButton1Down Event to the Activated One. Activated works on all Devices as far as i know, Maybe also try creating a variable for your RemoteEvent - Preferably at the top of your script.

-- Other variables above... Make sure to also reference your button
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CompleteMorphEvent = ReplicatedStorage:WaitForChild("CompleteMorphEvent")
local Camera = workspace.CurrentCamera or workspace:WaitForChild("Camera")
local Button = YourButton -- Change it with your button

Button.Activated:Connect(function()
	Camera.CameraType = Enum.CameraType.Fixed
	CompleteMorphEvent:FireServer()
	task.wait(1)
	Camera.CameraType = Enum.CameraType.Custom
	print('Set!')
end)
1 Like

But when I remove the task.wait(1), it runs perfectly fine.

I’ll try that, thanks!


I just tried it and it works completely fine for me. Seems like something’s wrong with your Roblox Studio.

Are you sure that the server is actually receiving the remote event fire?

It’s solved, when the remote event fired, the morph reset the character causing the local script also to get reset and start from the beginning, that’s why it didn’t run because of the delay. Thanks everyone for their help.

1 Like