MaxHealth isn't working when GuiButton is clicked

I’m making a “class” system, where if the player clicks one of the buttons. It changes their maxhealth.

When I tested it, the maxhealth and health were changed to 125. But when the enemy attacked the player, it only took 4 hits (25 dmg) to kill the player. The enemy is an npc using :TakeDamage() to damage the player.

I’ve tried looking as hard I could for a few hours but I couldn’t find anything relating to this.

Hierarchy (Using Image Buttons)
image

Health Bar Local Script in StarterGui

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid.HealthChanged:Connect(function(Damage)	
	script.Parent.Size = UDim2.new(Damage / humanoid.MaxHealth, 0, 1, 0)
end)

Juggernaut Local Script in StarterGui

local button = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local picked = game.ReplicatedStorage.ClassFolder.PickedClass --Bool Value to determine when player picked a class
local pickedJugg = game.ReplicatedStorage.ClassFolder.JuggernautBool --Bool Value to detect that player chose the juggernaut class

button.MouseButton1Click:Connect(function()
	local human = char:FindFirstChild("Humanoid")
	picked.Value = true
	pickedJugg.Value = true
	
	human.MaxHealth += 25
	human.Health += 25
	
	button.Parent.Parent.ClassFrame.Visible = false
end)

Sprinter Local Script in StarterGui

local button = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local picked = game.ReplicatedStorage.ClassFolder.PickedClass
local pickedSprinter = game.ReplicatedStorage.ClassFolder.SprinterBool

button.MouseButton1Click:Connect(function()
	local human = char:FindFirstChild("Humanoid")
	picked.Value = true
	pickedSprinter.Value = true
	
	human.Health-= 25
	human.MaxHealth-= 25
	button.Parent.Parent.ClassFrame.Visible = false
end)

Normal Script makes the class frame not visible.

You’re using a LocalScript, which runs on the client, and its changes will not be reflected on the server. This is why the character dies “prematurely”; the server manages the health, and it still acknowledges the old MaxHealth value.

I would recommend to use a RemoteEvent, where the client can request to change their class, and the server would change the player’s max health accordingly.

Ah ok, so I could put a remote event in replicated storage and fire it from the local script and to a server script? Also where would I put the server script, in StarterCharacterScripts?

StarterCharacterScripts is typically where you would put local scripts related to the character, I would place a server script in ServerScriptService.

Alright, thank you for your help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.