Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't this health regeneration script work?

Asked by
JJ_B 250 Moderation Voter
6 years ago

I am making a Local Script that calls a function when your health gets below 25. The script works fine in F6 studio test mode, but online it simply refuses to work. The code is:

char.Humanoid.Changed:connect(function(prop)
    local hum = char:WaitForChild("Humanoid")
    if prop == "Health" then
        print("prop gotten")
        if prop <= 25 then
        regenerate()
        end
end
end)

char is defined as:

local lp = game.Players.LocalPlayer
local char = lp.Character
if not char or not char.Parent then
    char = lp.CharacterAdded:wait()
end

It is definitely the if prop <= 25 then line that is the problem, as prints have told me. Why would this line work in studio, but not online? Please help.

1 answer

Log in to vote
0
Answered by 6 years ago

First off, there is an event named HealthChanged, which would be more wise to use. Next, this is a local script (or else would not work in with game.Players.LocalPlayer) -- changing properties in a local script will only work in studio if your game is Fe. There are two ways to go about this. If ALL players have this regeneration, then a server script would work better;

game.Players.PlayerAdded:Connect(function(Plr)
    Plr.CharacterAdded:Connect(function(Char)
        Char.Humanoid.HealthChanged:Connect(function()
            if Char.Humanoid.Health <= 25 then
                regenerate() -- Make sure to define this function somewhere (I'm assuming you already have)
            end
        end)
    end)
end)

On the other hand, if you need only certain players to have this ability, and it is detected through a local script with no efficient way to do it with a server script, you can use a remote event. Simply add a remote event to ReplicatedStorage, and define it in the local script.

remote:FireServer()

Then, a server script within ServerScriptStorage:

remote.OnServerEvent:Connect(function(Player)
    if Player.Character.Humanoid.Health <= 25 then -- add in any other ways to make sure the player didn't exploit to call this remote event
        regenerate() -- Again, define this somewhere.
    end
end)

I hope I helped, and have a good one!

0
My game is unfortunately not FE, but simply changing the Changed event to HealthChanged seems to have fixed it. Thanks! JJ_B 250 — 6y
Ad

Answer this question