How do I make it so that any player that joins during this script does not break the script

local sound = workspace["Time Stop"]
local sound2 = workspace["Jumpscare sound effect"]
local sound3 = workspace["Clock Ticking"]
local sound4 = workspace["Lethalovania"]
local lighting = game:GetService("Lighting")
local SSS = game:GetService("ServerScriptService")

while true do
	sound2:Play()
	task.wait(3)
	sound:Play()
	SSS.BlasterDupeScript.Disabled = true
	SSS.BoneSpinsScript.Disabled = true
	SSS.BoneDupeScript.Disabled = true
	SSS.AxesDupeScript.Disabled = true
	SSS.ChungDupeScript.Disabled = true
	local plrs = game.Players:GetPlayers()
	local colorCorrection = lighting.ColorCorrection
	for i = 1,100 do
		colorCorrection.Contrast -= 0.02
		sound4.PlaybackSpeed -= 0.01
		for _, plr in pairs(game.Players:GetPlayers()) do
			plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
			plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
			task.wait(0.01)
			game.Players.PlayerAdded:Connect(function(player)
				plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
				plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
			end)
		end
		task.wait(0.01)
	end
	sound3:Play()
	sound3.Looped = true
	for i = 1,200 do
		for _, plr in pairs(game.Players:GetPlayers()) do
			plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
			plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
			game.Players.PlayerAdded:Connect(function(player)
				plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
				plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
			end)
			task.wait(0.005)
		end
	end
	sound3.Looped = false
	SSS.BlasterDupeScript.Enabled = true
	SSS.BoneSpinsScript.Enabled = true
	SSS.BoneDupeScript.Enabled = true
	SSS.AxesDupeScript.Enabled = true
	SSS.ChungDupeScript.Enabled = true
	for i = 1,100 do
		sound4.PlaybackSpeed += 0.01
		colorCorrection.Contrast += 0.02
		for _, plr in pairs(game.Players:GetPlayers()) do
			plr.Character:WaitForChild("Humanoid").WalkSpeed = 50
			plr.Character:WaitForChild("HumanoidRootPart").Anchored = false
			game.Players.PlayerAdded:Connect(function(player)
				plr.Character:WaitForChild("Humanoid").WalkSpeed = 50
				plr.Character:WaitForChild("HumanoidRootPart").Anchored = false
			end)
			task.wait(0.01)
		end
	end
	task.wait(35)
end

For context, this script is a global serverscript that freezes everyone if they are ingame or have joined in the game. Here’s the problem,

plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
plr.Character:WaitForChild("HumanoidRootPart").Anchored = true

or

plr.Character:WaitForChild("Humanoid").WalkSpeed = 50
plr.Character:WaitForChild("HumanoidRootPart").Anchored = false

These two run even if a player hasn’t fully loaded in the game yet and I do not know how to adjust the script so that anybody that joins ingame while the script is active does not break it, because from what I know the script will attempt to find the player character’s Humanoid/HumanoidRootPart, regardless if the player has loaded in or not, which causes the script to break and I do not know how to counteract this.

game.Players.PlayerAdded:Connect(function(player)
		plr.Character:WaitForChild("Humanoid").WalkSpeed = 50			
plr.Character:WaitForChild("HumanoidRootPart").Anchored = false		
end)

or the opposite of it also doesn’t work. I’ve also tried waiting for the player’s character but it just returns to a infinite yield, which also breaks the script, and putting

game.Players.PlayerAdded:Connect(function(player)
		plr.Character:WaitForChild("Humanoid").WalkSpeed = 50			
plr.Character:WaitForChild("HumanoidRootPart").Anchored = false		
end)

before

for _, plr in pairs(game.Players:GetPlayers()) do
      ...
end

.
Is there any way I can make it so that the script both affects the player’s humanoid properties/ anchor the humanoidrootpart for both the players who are ingame, and those who have joined but haven’t fully loaded yet not break the script? If anyone would like to respond, please do so! I’d appreciate it.

1 Like

try using plr.CharacterAdded:Wait()

I tried that but the script unexpectedly stopped without any errors after I did

game.Players.PlayerAdded:Connect(function(player)
			player.CharacterAdded:Wait():FindFirstChild("Humanoid").WalkSpeed = 50
			player.CharacterAdded:Wait():FindFirstChild("HumanoidRootPart").Anchored = false
		end)

or

game.Players.PlayerAdded:Connect(function(player)
			player.CharacterAdded:Wait():FindFirstChild("Humanoid").WalkSpeed = 0
			player.CharacterAdded:Wait():FindFirstChild("HumanoidRootPart").Anchored = true
		end)

Looks like I’m missing something. No errors popped up in the developer console, nor did the script completely finish and was stuck on one part.

Update: got an error at Line 27, where it said index nil with :WaitForChild. That means the lines that have :WaitForChild continue still running for players even if their model hasn’t fully loaded yet.

I may be wrong but I don’t think you can use waitforchild on characteradded maybe try doing

game.Players.PlayerAdded:Connect(function(player)
			player.CharacterAdded:Wait(0.5)
                        player.Character:FindFirstChild("Humanoid").WalkSpeed = 0
                        player.Character:FindFirstChild("HumanoidRootPart").Anchored = true
			
		end)

Tried that, but the script after a player joins during the script doesn’t show any error and doesn’t progress further.

try adding prints and telling me where it stops working

It stops working after a player joined. I added prints for all 3 and after a player joined, the print stopped appearing.

Is it in a while loop? If so then I think you need to take it out.

while true do
	sound2:Play()
	task.wait(3)
	sound:Play()
	SSS.BlasterDupeScript.Disabled = true
	SSS.BoneSpinsScript.Disabled = true
	SSS.BoneDupeScript.Disabled = true
	SSS.AxesDupeScript.Disabled = true
	SSS.ChungDupeScript.Disabled = true
	local plrs = game.Players:GetPlayers()
	local colorCorrection = lighting.ColorCorrection
	for i = 1,100 do
		colorCorrection.Contrast -= 0.02
		sound4.PlaybackSpeed -= 0.01
		game.Players.PlayerAdded:Connect(function(player)
			player.CharacterAdded:Wait(0.5)
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 0
			player.Character:FindFirstChild("HumanoidRootPart").Anchored = true
		end)
		for _, plr in pairs(game.Players:GetPlayers()) do
			plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
			plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
		end
		task.wait(0.01)
		print("ONGOING")
	end
	sound3:Play()
	sound3.Looped = true
	for i = 1,200 do
		game.Players.PlayerAdded:Connect(function(player)
			player.CharacterAdded:Wait(0.5)
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 0
			player.Character:FindFirstChild("HumanoidRootPart").Anchored = true
		end)
		for _, plr in pairs(game.Players:GetPlayers()) do
			plr.CharacterAdded:Wait("Humanoid").WalkSpeed = 0
			plr.CharacterAdded:Wait("HumanoidRootPart").Anchored = true
		end
		task.wait(0.005)
		print("ONGOING")
	end
	sound3.Looped = false
	SSS.BlasterDupeScript.Enabled = true
	SSS.BoneSpinsScript.Enabled = true
	SSS.BoneDupeScript.Enabled = true
	SSS.AxesDupeScript.Enabled = true
	SSS.ChungDupeScript.Enabled = true
	for i = 1,100 do
		sound4.PlaybackSpeed += 0.01
		colorCorrection.Contrast += 0.02
		game.Players.PlayerAdded:Connect(function(player)
			player.CharacterAdded:Wait(0.5)
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 50
			player.Character:FindFirstChild("HumanoidRootPart").Anchored = true
			return
		end)
		for _, plr in pairs(game.Players:GetPlayers()) do
			plr.Character:WaitForChild("Humanoid").WalkSpeed = 50
			plr.Character:WaitForChild("HumanoidRootPart").Anchored = false
		end
		task.wait(0.01)
		print("ONGOING")
	end
	task.wait(35)
end

The place where these lines of code are running in is in a for i loop.

One example of this in the code is this,

for i = 1,200 do
		game.Players.PlayerAdded:Connect(function(player)
			player.CharacterAdded:Wait(0.5)
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 0
			player.Character:FindFirstChild("HumanoidRootPart").Anchored = true
		end)
		for _, plr in pairs(game.Players:GetPlayers()) do
			plr.CharacterAdded:Wait("Humanoid").WalkSpeed = 0
			plr.CharacterAdded:Wait("HumanoidRootPart").Anchored = true
		end
		task.wait(0.005)
		print("ONGOING")
	end

Try removing the for loop from the while loop(if possible)

Nevermind, it breaks at this part.

for i = 1,200 do
		game.Players.PlayerAdded:Connect(function(player)
			player.CharacterAdded:Wait(0.5)
			player.Character:FindFirstChild("Humanoid").WalkSpeed = 0
			player.Character:FindFirstChild("HumanoidRootPart").Anchored = true
		end)
		for _, plr in pairs(game.Players:GetPlayers()) do
			plr.CharacterAdded:Wait("Humanoid").WalkSpeed = 0
			plr.CharacterAdded:Wait("HumanoidRootPart").Anchored = true
		end
		task.wait(0.005)
		print("ONGOING")
	end

Also, honestly, there is no point putting the playeradded event there when it automatically fires anyways

In case you haven’t read the context of what I’m trying to make, I will repeat it once again. There’s a reason why for that. I am trying to make a serverscript that freezes everyone if they are ingame or have joined in the game.

These two run even if a player hasn’t fully loaded in the game yet and I do not know how to adjust the script so that anybody that joins ingame while the script is active does not break it, because from what I know the script will attempt to find the player character’s Humanoid/HumanoidRootPart, regardless if the player has loaded in or not, which causes the script to break and I do not know how to counteract this.

The reason for the PlayerAdded event is to ensure the script does not automatically anchor a player’s HumanoidRootPart or modify Humanoid as soon as the player joins and the player model hasn’t fully loaded yet.

I have a early assumption once again to think the script was fixed.

for _, plr in pairs(game.Players:GetPlayers()) do
			plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
			plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
		end

These parts immediately index nil when a player joins ingame while the script is active. This is why I needed a solution that made it so that if a player joins they don’t trigger that part instantly to ensure that the script doesn’t break when a player joins.

try:

for _, plr in pairs(game.Players:GetPlayers()) do
               plr.CharacterAdded:Connect(function(char)
			char:WaitForChild("Humanoid").WalkSpeed = 0
			char:WaitForChild("HumanoidRootPart").Anchored = true
                     end)
		end

While that does work, I don’t want it so that the player has to respawn to be freezed in midair.

Does it not work the first time they spawn in?

While it does work, it does not give me the result I want, that being that the player is freezed midair ingame regardless if they have respawned or not and then being unanchored with a proper walkspeed later.

Change

for _, plr in pairs(game.Players:GetPlayers()) do
    plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
    plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
    task.wait(0.01)
    game.Players.PlayerAdded:Connect(function(player)
        plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
        plr.Character:WaitForChild("HumanoidRootPart").Anchored = true
    end)
end

into

game.Players.PlayerAdded:Connect(function(plr)
    local character = plr.Character or plr.CharacterAdded:Wait();

    character.Humanoid.WalkSpeed = 0;
    character.HumanoidRootPart.Anchored = true;
end)

for _, plr in game.Players:GetPlayers() do
    local character = plr.Character or plr.CharacterAdded:Wait();

    character.Humanoid.WalkSpeed = 0;
    character.HumanoidRootPart.Anchored = true;
end

Problems with the first script:

  • There is no guarantee the player’s character has loaded in.
    • If the character is not loaded in yet, will result in error “attempt to index nil with WaitForChild” (or similar, I’m not exactly sure)
  • You are creating a connection to Players.PlayerAdded every iteration, when it should only be done once, outside the loop.
  • In a fixed script, there is no need for WaitForChild since Player.CharacterAdded guarantees that the Humanoid and HumanoidRootPart are already loaded.

So if I understand correctly, you don’t want it to happen every time they respawn?