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

How to fix lag in this script It runs a loop to check if a player died?

local Player = game.Players.LocalPlayer
local RepStorage = game:GetService("ReplicatedStorage")
local Remote = RepStorage:WaitForChild("DeathRemote")

local function FireRemote()
Remote:FireServer()
end

while true do 
	wait(1)
	if game.Players.LocalPlayer.Character.Humanoid.Health <= 0 then
		FireRemote()
	end
end


-- how i wanted to fix it
Player.Character.Humanoid.Health.Changed:Connect(function()
	print("Health Changed!")
end)

So this runs a loop every second to check if a player has died this can cause lag , I already have experienced lag currently im doing some lag work for my game can someone help me?

1 answer

✓ Accepted answer

Okay, bro! Humanoid.Health is a property of Humanoid and it has no changed event.

So you may change it to:

local hum = Player.Character.Humanoid

local previousHealth = hum.Health

hum.Changed:Connect(function()
	if previousHealth ~= hum.Health then
		print("Changed health!")
		previousHealth = hum.Health
		--fire remote as what you wanted
		if hum.Health <= 0 then
			FireRemote()
		end
	end
end)

And fire some remotes...

3 comments

0

I'm late, but Humanoid.HealthChanged is real.

0

Well, HealthChanged is a native function of Humanoid, but not belongs to the Health variable of the humanoid class.

0

I like Changed more, although it has a sightly higher firing frequency than the HealthChanged signal.

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