Script not printing?

Hello there users of devforum. I have come across an error within this script that whenever the players health is below or equal to 70 it doesn’t print. Any ideas?

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
		
		local humanoid = Char:WaitForChild("Humanoid")
		
		humanoid.HealthChanged:Connect(function()
			if humanoid.Health <= 70 then
				print("Player Is Below 70 Health")

Are you testing this in studio? In studio the player loads before the server.

1 Like

The best thing to do is something like this:

local onPlayerLoaded = function(player)
	...
end;

game.Players.PlayerAdded:Connect(onPlayerLoaded);
for _, p in pairs(game:GetService("Players"):GetPlayers()) do
	onPlayerLoaded(p);
end;

This will ensure that all the players are loaded, even if they join before like in studio

2 Likes

try this

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
		local humanoid = Char:WaitForChild("Humanoid")
		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health <= 70 then
				print("Player Is Below 70 Health")
end
end)
end)
end)
1 Like

Like STORMGAMESYT7IP is saying the Player.CharacterAdded:Connect(function(Char) is not fast enough to get the first character alot of times so you need an initial setup then do the connection for any futher best way i have found is use a function to setup your character outside of them like below. also added all your ends back like Ayoub had

function SetupCharacter(Player,Char)  -- setup your character stuff in this function
	local humanoid = Char:WaitForChild("Humanoid")
	humanoid.HealthChanged:Connect(function()
		if humanoid.Health <= 70 then
			print("Player Is Below 70 Health")
		end
	end)
end

game.Players.PlayerAdded:Connect(function(Player)
	local Char = Player.Character or Player.CharacterAdded:Wait()  -- you need to get character to run initial setup when server runs
	SetupCharacter(Player,Char)  -- initial for the character when server runs
	Player.CharacterAdded:Connect(function(Char)  -- this isn't fast enough to get first character but will get any if they reset or change character later
		SetupCharacter(Player,Char)  -- call for any reset or changed characters
	end)
end)
1 Like

Usually the event is not fast enough to get the player character on the first time because the script has to wait for some variables or algorithms placed before the event in the code, for example some waits you previously put in the code… Just try to move the events at the top of the script and let us know if it works…

1 Like

Sorry for the very late reply, but It didn’t seem to print…

This to didn’t print it nor work.

No I am loading this via client and server
image

Did you to put the code in and tried it?

Like this?
image

you already have the player in the “player” variable, so then do player.CharacterAdded and not .PlayerAdded:

... function(player)
	player.CharacterAdded:Connect(...)
end;

I just did that and it’s still not printing…

Then do this as well:

local onPlayerLoaded = function(player)
	local onCharacterLoaded = function(Char)
		local humanoid = Char:WaitForChild("Humanoid")
		
		humanoid.HealthChanged:Connect(function()
			print("Health changed");
			if humanoid.Health <= 70 then
				print("Player Is Below 70 Health")
			end;
		end);
	end;

	player.CharacterAdded:Connect(onCharacterLoaded);
	if(player.Character) then onCharacterLoaded(player.Character) end;
end;
1 Like

Does “Health changed” print?

Actually see if “Nice” and “Nice x2” prints:

local onPlayerLoaded = function(player)
	print("Nice");
	local onCharacterLoaded = function(Char)
		print("Nice x2");
		local humanoid = Char:WaitForChild("Humanoid")
		
		humanoid.HealthChanged:Connect(function()
			print("Health changed");
			if humanoid.Health <= 70 then
				print("Player Is Below 70 Health")
			end;
		end);
	end;

	player.CharacterAdded:Connect(onCharacterLoaded);
	if(player.Character) then onCharacterLoaded(player.Character) end;
end;

Nope Not even “nice” is printing…

Can you send me all your code currently?

Well Ive straightup copy n pasted ur script…

local onPlayerLoaded = function(player)
	print("Nice");
	local onCharacterLoaded = function(Char)
		print("Nice x2");
		local humanoid = Char:WaitForChild("Humanoid")

		humanoid.HealthChanged:Connect(function()
			print("Health changed");
			if humanoid.Health <= 70 then
				print("Player Is Below 70 Health")
			end;
		end);
	end;

	player.CharacterAdded:Connect(onCharacterLoaded);
	if(player.Character) then onCharacterLoaded(player.Character) end;
end;

Well, that is why it isn’t working. I assumed that you would keep the other part. Should have clarified, sorry.

local onPlayerLoaded = function(player)
	print("Nice");
	local onCharacterLoaded = function(Char)
		print("Nice x2");
		local humanoid = Char:WaitForChild("Humanoid")
		
		humanoid.HealthChanged:Connect(function()
			print("Health changed");
			if humanoid.Health <= 70 then
				print("Player Is Below 70 Health")
			end;
		end);
	end;

	player.CharacterAdded:Connect(onCharacterLoaded);
	if(player.Character) then onCharacterLoaded(player.Character) end;
end;

game.Players.PlayerAdded:Connect(onPlayerLoaded);
for _, p in pairs(game:GetService("Players"):GetPlayers()) do
	onPlayerLoaded(p);
end;
2 Likes