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")
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
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)
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)
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…
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;
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;