Hello there. I am making a +1 health every second game, and I seem to have found a problem.
Every second, the player’s MaxHealth becomes equal to a IntValue that is located inside the player’s leaderstats. The IntValue is rising by +1 every second.
For some reason, whenever the player joins or respawns, their health becomes 100 instead of the MaxHealth. Sometimes, because of the vast difference of the health and the MaxHealth, the player dies.
I assume this happens because roblox may instantly set the player’s health to 100.
game.Players.PlayerAdded:Connect(function(player)
while true do
task.wait(1)
local plaour = game.Players[player.Name]
local char = plaour.Character
char:WaitForChild("Humanoid").MaxHealth = player:WaitForChild("leaderstats"):WaitForChild("HealthValue").Value
char.Humanoid.Health += player.leaderstats.Wins.Value
end
end)
My technique to get the player’s character is a bit atrocious, so it’d be nice if you can give me another way to do that.
Anyways, I don’t really want to make it so that the health becomes the MaxHealth every second, because then, that would make all the damage bricks and obstacles pointless.
Remember to read all the notes. Also added your 1 health per second loop.
local Default_Health = 110 -- Sets the default value of characters MaxHealth
-- Connect up a function, that runs when a player joins the game
game.Players.PlayerAdded:Connect(function(Player)
-- Create leaderstats
local leaderstats = Instance.new("leaderstats")
local V_Health = Instance.new("IntValue")
V_Health.Name = "MaxHealthValue"
V_Health.Value = Default_Health
V_Health.Parent = leaderstats
leaderstats.Parent = Player -- Always parent the leaderstats folder itself lastly
-- Connect up a function, that runs everytime our V_Health changes value
V_Health:GetPropertyChangedSignal("Value"):Connect(function()
local Character = Player.Character
-- If player has a character currently present
if Character then
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
-- If character has a humanoid currently present
if Humanoid then
-- Change humanoid MaxHealth to the value of V_Health
Humanoid.MaxHealth = V_Health.Value
Humanoid.Health = V_Health.Value
end
end
end)
-- Connect up a function, that runs everytime the player character loads
Player.CharacterAdded:Connect(function(Character)
-- Wait for humanoid to be present
local Humanoid = Character:WaitForChild("Humanoid")
-- Change humanoid MaxHealth to the value of V_Health
Humanoid.MaxHealth = V_Health.Value
Humanoid.Health = V_Health.Value
end)
-- Load their character after we've setup all the functions
-- Make sure to disable "CharacterAutoLoads" in the "Players" tab in the Explorer
Player:LoadCharacter()
end)
-- We spawn a new "thread" here for the while loop to run on, so it does not yield any possible scripting below this loop, if we later on add any.
spawn(function()
-- We create a endless loop. When it have reached the end of the loop, it waits 1 second before doing it all again
while task.wait(1) do
-- We create a loop that loops through all players
for _, Player in pairs(game.Players:GetPlayers()) do
local leaderstats = Player:FindFirstChild("leaderstats")
-- If leaderstats was not present at the time of checking the player, we continue to next player in the loop
if not leaderstats then continue end
local V_Health = leaderstats:FindFirstChild("MaxHealthValue")
-- If V_Health was not present at the time of checking the leaderstats, we continue to next player in the loop
if not V_Health then continue end
-- We add 1 to their V_Health value
V_Health.Value += 1
end
-- Reached the end, now waits 1 second before doing it all again
end
end)
Unfortunately, this didn’t work. What I was trying to achieve was making the player’s health become the player’s MaxHealth or the HealthValue in leaderstats once they join.
But this just repeated what my own script did, and I’m honestly losing hope on if this is even possible.
I have a datastore2 thingy I made to save the HealthValue thing that I didn’t show in the script. I didn’t really think it was relevant.
Also I don’t think we really need to worry about that right now, the main problem at hand is that, whenever the player joins or their character is added, their health instantly goes to 100 instead of going to the MaxHealth.
The reason for the player’s health is set to 100, is that when a character spawns in, the default MaxHealth is 100. When you change that MaxHealth to lets say 200, you also have to set their Health to MaxHealth afterwards.
Added this little piece of code and I want to facepalm right now:
Humanoid.Health = V_Health.Value
I can’t believe I spent like 2 days trying to fix this issue that seemingly couldn’t be fixed.
It’s odd because I’m pretty sure I tried this strategy and was confused why it didn’t work.
Thanks btw.