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
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?
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.
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