Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't this script work? No errors are produced. Only the first "Print" is produced in output

A quick rundown, it's a morph system for an upcoming dinosaur game. This is a footstep system for when the player is near the dinosaurs (Screenshake)

However,

The code below doesn't work. No errors are produced, and the only thing that prints is "Phase1" Phase 2+ do not print. Any help would be appreciated, thanks! THIS IS NOT A LOCALSCRIPT

print("Phase1")
game.Players.PlayerAdded:Connect(function(Player)
	print("Phase2")
	script.Parent.Touched:Connect(function(onhit)
		if onhit:IsDescendantOf(script.Parent.Parent) then
		else
			if script.Parent.Parent.IsWalking.Value ==true then
				local DT = Player.Character:WaitForChild("DinosaurType")
				if DT then
					if DT.Value == "Tyrant" then
					else
						if onhit:IsDescendantOf(Player.Character) then
							for i = 1,20 do
								local hum = Player.Character.Humanoid
								print("Phase3")
								local a = math.random(-100,100)/100
								local b = math.random(-100,100)/100
								local c = math.random(-100,100)/100
								hum.CameraOffset = Vector3.new(a,b,c)
								wait()
							end
						end
					end
				else
					for i = 1,20 do
						local hum = Player.Character.Humanoid
						print("Phase2")
						local a = math.random(-100,100)/100
						local b = math.random(-100,100)/100
						local c = math.random(-100,100)/100
						hum.CameraOffset = Vector3.new(a,b,c)
						wait()
					end
				end
			end
		end
	end)
end)

1 answer

The game runs both the server and client at the same time, so sometimes what happens is that the player gets added before the server script is able to run. So what you could do is use a loop that loops through all players in the game instead.

You could do something like this (not the best but it should work):

local function plrAdded(player)
	--your code
end

game.Players.PlayerAdded:Connect(plrAdded) --regular PlayerAdded

for i, plr in pairs(game.Players:GetChildren()) do
	plrAdded(plr) --loop through and add player
end

1 comment

0

+1. Note: Roblox recommends using ":GetPlayers()" instead of ":GetChildren()", though I suppose this only matters if someone stores things in Players (which you shouldn't do).

This discussion is preserved as it was. New answers, comments, and votes are closed.