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

Changing player health using a gui button not working? (FE)

Asked by 5 years ago
Edited 5 years ago

So, I'm trying to change a player's health using a gui button ; If they have enough coins. Scripts:

--Local Script--
local button = script.Parent.Button

button.MouseButton1Click:Connect(function()
    if plr.leaderstats.Coins.Value >= button.Coins.Value then
        folder.ChangeHealth:FireServer(20,30) -- 20 is hp, 30 is the coin value
    end
end)

--Server Script--
game.ReplicatedStorage.Gui.Menu.ChangeHealth.OnServerEvent:Connect(function(plr,hp,m)
    plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - m

    local ch = plr.Character
    ch.Humanoid.MaxHealth = ch.Humanoid.MaxHealth + hp
end)

After i typed this, and tested it, my coin value goes to a negative number. Why?

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago

Thats because you are not taking button.Coins.Value (which I assume is the cost of changing your hp) and sending that through the remoteevent.

Also you should do ANOTHER check on the server to make sure a hacker isnt abusing this system.

--Local Script--
local button = script.Parent.Button

button.MouseButton1Click:Connect(function()
    if plr.leaderstats.Coins.Value >= button.Coins.Value then
        folder.ChangeHealth:FireServer(20,button.Coins.Value) -- 20 is hp, 30 is the coin value
    end
end)

--Server Script--
game.ReplicatedStorage.Gui.Menu.ChangeHealth.OnServerEvent:Connect(function(plr,hp,m)
   if plr.leaderstats.Coins.Value >= m then
    plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - m

    local ch = plr.Character
    ch.Humanoid.MaxHealth = ch.Humanoid.MaxHealth + hp

    --delete this if you want but your only changing the maxhealth here
    ch.Humanoid.Health= ch.Humanoid.Health+ hp
   end
end)

Also made it change its health and not just maxhealth.

Ad

Answer this question