LocalScript in StarterGui
local maxHealth = 120 -- Used to define the max amount of health for players.
local player = game.Players.LocalPlayer --Can only be used in local scripts
local event = game.ReplicatedStorage:WaitForChild('HealthChanged') --Requires RemoteEvent in Replicated Storage called HealthChanged
player.CharacterAdded:connect(char) --When the character is added
local humanoid = char:WaitForChild('Humanoid') --Wait for Humanoid
humanoid.HealthChanged:connect(function() --When the humanoid's health changes
if humanoid.Health > maxHealth then --Check for if it's higher than the maxHealth
event:FireServer(humanoid) --Fire the server with the humanoid
end
end)
end)
Script in ServerScriptStorage
local maxHealth = 120 -- Used to define the max amount of health for players.
local event = game.ReplicatedStorage:WaitForChild('HealthChanged') --Grabs event
event.OnServerEvent:connect(function(player, humanoid) --When the event is triggered by client, grab the player and humanoid
humanoid.Health = maxHealth --Change the health to maxHealth
end)
