Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by
hokyboy 207 Moderation Voter
8 days ago
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

Log in to vote
1
Answered by 8 days ago
Edited 8 days ago

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...

0
I'm late, but Humanoid.HealthChanged is real. Unity_456 47 — 7d
0
Well, HealthChanged is a native function of Humanoid, but not belongs to the Health variable of the humanoid class. LinavolicaDev 540 — 7d
0
I like Changed more, although it has a sightly higher firing frequency than the HealthChanged signal. LinavolicaDev 540 — 7d
Ad

Answer this question