Issue freezing players before gamemode

I am making an fps gamemode. Before the gamemode starts I want to freeze players until the countdown is completed. I want it to be like call of duty. Here is the script I am currently using to try to do that. The problem I am having is that it only freezes one player and the rest of the players are free to move. I have tried several different approaches to this. My last attempt was a bindable event but that didnt work. Does anyone know what im doing wrong? (script is a module script btw in serverscriptservice)

– Bindable Event to freeze players when needed
local freezeEvent = Instance.new(“BindableEvent”)
freezeEvent.Name = “FreezePlayersEvent”
freezeEvent.Parent = script

local function freezePlayer(player)
if player.Character and player.Character:FindFirstChild(“Humanoid”) then
player.Character.Humanoid.WalkSpeed = 0
frozenPlayers[player] = true
end
end

local function unfreezePlayer(player)
if player.Character and player.Character:FindFirstChild(“Humanoid”) then
player.Character.Humanoid.WalkSpeed = 16
frozenPlayers[player] = nil
end
end

local function freezeAllPlayers()
for _, player in ipairs(Players:GetPlayers()) do
freezePlayer(player)
end
end

local function unfreezeAllPlayers()
for _, player in ipairs(Players:GetPlayers()) do
unfreezePlayer(player)
end
end

1 Like

hey there! your loop looks fine, however it can yield or stop if an error comes up in a function within that loop. have you checked any errors relating to frozenPlayers[player] = true? if only one person gets frozen, it would mean that the code most likely stops at that part due to an error. could you check the console for errors?

yes, sorry i forgot to include that it doesnt give any errors in the output.

If you are calling the function “freezeAllPlayers” and only one player is being frozen there may be a problem with the loop.

Try printing the players right below the ipairs loops in freezeAllPlayers. If it prints all of the player’s names then the problem isnt with the loop.

Then do a print in the freezePlayer function and see if it is called only once or multiple times and if the player variable is the same person or a different person.

New FreezeAllPlayers function:

local function freezeAllPlayers()
for _, player in ipairs(Players:GetPlayers()) do
print(player)
freezePlayer(player)
end
end

The goal here is to isolate the problem since there are no errors

ok, i added the prints one that says attempting to freeze player and one when the player is frozen. I tested it out with 4 players and it only printed that it froze the first 2. But in reality none of the characters were frozen and i could still move each of them.

the game starts almost immediately when the players join. could it be an issue of not logging new players?

probably make a event that calls the functions inside that module script

I did a small test and got it to freeze the players.

When I encounter problems like these in complex systems, I try to narrow it down.

If I were you I would disable the function “unfreezeAllPlayers” and “unfreezePlayer” so there is no possibility of them being unfreezed.

I would add a wait(5) or freeze all the players when you press a button so you know for sure that everybody is loaded in the game before the function runs.

Here is my version that freezes everybody

local Players = game.Players

local function freezePlayer(player)
if player.Character and player.Character:FindFirstChild(“Humanoid”) then
player.Character.Humanoid.WalkSpeed = 0
– frozenPlayers[player] = true
end
end

local function freezeAllPlayers()
for _, player in ipairs(Players:GetPlayers()) do
freezePlayer(player)
end
end

script.Parent.MouseClick:Connect(function()
freezeAllPlayers()
end)

you could keep the unfreeze all players but check to see if their frozen and then it would be fine

Yes you could keep it.

I would just temporarily remove it so there is no chance of confusion and then add it back when the problem is solved.