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

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

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

✓ Accepted answer

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.

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