This is the code:
I want the character to remain still when the function is ran, wait for the loop to finish, and then be able to continue walking once ReturnToSpawn:FireServer() happens.
local function ReturnToSpawnFunc()
Humanoid.WalkSpeed = 0
print(Humanoid.WalkSpeed)
ReturnToSpawnGui.Parent.Enabled = false
ReturnToSpawnGui.Enabled = true
local TimeToCount = 11
while task.wait(1) and TimeToCount > 0 do
print("e")
TimeToCount -= 1
CountdownSound:Play()
countdown.Text = TimeToCount .." seconds"
end
ReturnToSpawnGui.Enabled = false
countdown.Text = ""
ReturnToSpawn:FireServer()
Humanoid.WalkSpeed = 16
end
script.Parent.ExitButton.MouseButton1Click:Connect(ReturnToSpawnFunc)
script.Parent.ExitButton.MouseButton2Click:Connect(ReturnToSpawnFunc)
character is defined earlier with:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
Now, when testing, when the ReturnToSpawnFunc() is ran the player is still able to move even though Humanoid.WalkSpeed = 0? I KNOW it is 0 BECAUSE it the Humanoid.WalkSpeed print prints out “0”.
I believe the character is still moving because you’re setting the walk speed on the local client/script, not the server. The server doesn’t acknowledge the walk speed change for the client, so the character still moves.
You’d need to change the walk speed on a server script so both the client and server agree, but other than that I think it’s fine.
I would still recommend making the walk speed change to a server script anyway. I think it’s safer the server and client agree if you want to do what you’re doing, otherwise you might get some janky behavior or inconsistencies.
I can see you’re using FireServer(), so you’re talking to a server script anyways. Considering remote events are passed the Player instance, you can get the character with Player.Character.
Server Script (in like server script service or something)
local Remote = game:GetService("ReplicatedStorage"):WaitForChild("StopMoving")
function StopMoving(player)
local Character = player.Character
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.WalkSpeed = 0
end
Remote.OnServerEvent:Connect(StopMoving)
Where your client script is
That said, you’d need to add a remote before the loop started.
local StopMovingRemote = game:GetService("ReplicatedStorage"):WaitForChild("StopMoving")
local function ReturnToSpawnFunc()
StopMovingRemote:FireServer() -- server sets walkspeed to 0
print(Humanoid.WalkSpeed) -- client agrees
ReturnToSpawnGui.Parent.Enabled = false
ReturnToSpawnGui.Enabled = true
local TimeToCount = 11
while task.wait(1) and TimeToCount > 0 do
print("e")
TimeToCount -= 1
CountdownSound:Play()
countdown.Text = TimeToCount .." seconds"
end
ReturnToSpawnGui.Enabled = false
countdown.Text = ""
ReturnToSpawn:FireServer() -- use the server script here to fix the walkspeed
-- using a similar method to the other one
end
script.Parent.ExitButton.MouseButton1Click:Connect(ReturnToSpawnFunc)
script.Parent.ExitButton.MouseButton2Click:Connect(ReturnToSpawnFunc)