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

How come this max health script wont work?

Asked by 4 years ago
Edited 4 years ago

This script makes it so when the remove event is fired you gain "Defense" and the more defense you have the higher your max health is.. But for some reason when a brand new player joins when they go to Fire said event their max health goes to 0 then when they do it again their max health goes up by 500(Which is what is suppose to happen)

OnServerEvent

local event = game.ReplicatedStorage:WaitForChild("Def")
local DefaultHealth = 100

event.OnServerEvent:Connect(function(player)
local Stats = player:WaitForChild("Stats")
local Strength = Stats:FindFirstChild("Defense")
local char = player.Character or player.CharacterAdded:wait()
if char then
local hum = char:FindFirstChild("Humanoid")
if hum then
hum.MaxHealth = DefaultHealth * Strength.Value
Strength.Value = Strength.Value + 5 -- Change 1 to what ever u want
end
end
end)

As shown by the video theres a new player, when they go to train there defense, their max health goes down to 0 but when they train it again the max health goes up by 500 on each press.

https://gyazo.com/64a8006cd5bea44eb3d8b709b43c7174

How do i make it so the player's health doesn't go down to 0?

1 answer

Log in to vote
1
Answered by 4 years ago

OK i found the problem

hum.MaxHealth = DefaultHealth * Strength.Value
Strength.Value = Strength.Value + 5

All i had to do was switch the 2 lines

Updated Script

local event = game.ReplicatedStorage:WaitForChild("Def")
local DefaultHealth = 100

event.OnServerEvent:Connect(function(player)
local Stats = player:WaitForChild("Stats")
local Strength = Stats:FindFirstChild("Defense")
local char = player.Character or player.CharacterAdded:wait()
if char then
local hum = char:FindFirstChild("Humanoid")
if hum then
Strength.Value = Strength.Value + 5
hum.MaxHealth = DefaultHealth * Strength.Value
end
end
end)
Ad

Answer this question