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 5 years ago
Edited 5 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

01local event = game.ReplicatedStorage:WaitForChild("Def")
02local DefaultHealth = 100
03 
04event.OnServerEvent:Connect(function(player)
05local Stats = player:WaitForChild("Stats")
06local Strength = Stats:FindFirstChild("Defense")
07local char = player.Character or player.CharacterAdded:wait()
08if char then
09local hum = char:FindFirstChild("Humanoid")
10if hum then
11hum.MaxHealth = DefaultHealth * Strength.Value
12Strength.Value = Strength.Value + 5 -- Change 1 to what ever u want
13end
14end
15end)

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 5 years ago

OK i found the problem

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

All i had to do was switch the 2 lines

Updated Script

01local event = game.ReplicatedStorage:WaitForChild("Def")
02local DefaultHealth = 100
03 
04event.OnServerEvent:Connect(function(player)
05local Stats = player:WaitForChild("Stats")
06local Strength = Stats:FindFirstChild("Defense")
07local char = player.Character or player.CharacterAdded:wait()
08if char then
09local hum = char:FindFirstChild("Humanoid")
10if hum then
11Strength.Value = Strength.Value + 5
12hum.MaxHealth = DefaultHealth * Strength.Value
13end
14end
15end)
Ad

Answer this question