Custom Hp bar Bug

hey, so as i said in my last post, i have a lot of bugs in my game that i have no idea how to fix them, i tried so many ways to fix it but nothing worked. i’m not a good Scripter already telling…

to make it clear, i have a RPG game that every level it gives you 4 max hp, the starting hp (level 1) is 20, so i made a formula , LEVEL*4+16

in the script i named my Level as LOVE , so Level = LOVE

and now im testing my hp bar when i’m level 100

so my bug is:
https://gyazo.com/4c966fbe69996c9f5171b38b501b6fe3
basically , sometimes i start with 20 maxhp while i suppose to start with 416, because the formula [100*4+16], but only after i die it changes to 416… and that’s not all

second bug with hp bar:
https://gyazo.com/d36348827934603b778fdb89f131271b
[it’s recorded after i died once already]

so yea, as you can see the hp won’t go down in the UI, but it still kills you…

my hp bar script script:

local remote = game.ReplicatedStorage.PlayerSpawned
local player = game.Players.LocalPlayer
local human = player.Character:WaitForChild("Humanoid")

wait(1)

script.Parent.Frame:TweenSize(UDim2.new(100/100,0,1,0), "Out", "Quad", .25)
script.Parent.TextLabel.Text = (human.Health .. "/" .. human.MaxHealth)



human.HealthChanged:Connect(function(nh)
	local human = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
 local newHealth = player.Character.Humanoid.Health
 local maxhp = human.MaxHealth
  if maxhp then
	spawn (function()
		script.Parent.Frame:TweenSize(UDim2.new(newHealth/player.Character.Humanoid.MaxHealth,0,1,0), "Out", "Quad", .25)
	end)


   end
   script.Parent.TextLabel.Text = math.floor(newHealth+0.5) .. "/" .. maxhp
end)

remote.OnClientEvent:Connect(function()
	local human = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
	script.Parent.Frame:TweenSize(UDim2.new(100/100,0,1,0), "Out", "Quad", .25)
	script.Parent.TextLabel.Text = (human.Health .. "/" .. human.MaxHealth)
end)

the hp changes from a script in ServerScriptService:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
	wait()
    char.Humanoid.MaxHealth = plr.GasterVoid.LOVE.Value*4 + 16
	char.Humanoid.Health = char.Humanoid.MaxHealth
	game.ReplicatedStorage.PlayerSpawned:FireClient(plr)
    end)
end)

here’s where everything placed and all:

the first script placed in LocalScript in HPbar frame
and the second script placed in ServerScriptService in MAX_HP

hope it was clear and understandable, thanks in advance :slight_smile:

I think it might be an issue with your kill brick? You had to touch it a few times, try printing a lot of things in there or something.

i made the brick do 200 damage, it has nothing to do with my kill brick, and it has wait(2) in it, so this is why it had a delay with the damage

Ok, I think I got it, but I might be wrong.

I think that your event is connected to the old humanoid, so when the player is respawned it is no longer connected. Try reconnecting every time the character is spawned, or just put the script in StarterCharacterScripts instead of the Gui.

Hope this helps! :smile:

i tried to do the second option,with the StarterCharacterService, but it didn’t worked…
and i have no idea how to do reconnecting option, can you tell me how you do it?


local remote = game.ReplicatedStorage.PlayerSpawned

remote.OnClientEvent:Connect(function()
	
local player = game.Players.LocalPlayer
local human = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
script.Parent.Frame:TweenSize(UDim2.new(100/100,0,1,0), "Out", "Quad", .25)
script.Parent.TextLabel.Text = (human.Health .. "/" .. human.MaxHealth)

wait(1)

script.Parent.Frame:TweenSize(UDim2.new(100/100,0,1,0), "Out", "Quad", .25)
script.Parent.TextLabel.Text = (human.Health .. "/" .. human.MaxHealth)



human.HealthChanged:Connect(function(nh)
	local human = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
 local newHealth = player.Character.Humanoid.Health
 local maxhp = human.MaxHealth
  if maxhp then
	spawn (function()
		script.Parent.Frame:TweenSize(UDim2.new(newHealth/player.Character.Humanoid.MaxHealth,0,1,0), "Out", "Quad", .25)
	end)


   end
   script.Parent.TextLabel.Text = math.floor(newHealth+0.5) .. "/" .. maxhp
end)



end)
1 Like

Hey!, you solved my second problem :smiley:, but the first bug still exist sadly, thank you so much!

Let me fix that, give me a minute…

Are you sure the gaster void thing loads on the first time? Can you try printing out the level value?

yea it takes some time for gastervoid to load, i noticed that… i have no idea what to do with it

Try this:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		plr:WaitForChild("GasterVoid")
	    char.Humanoid.MaxHealth = plr.GasterVoid.LOVE.Value*4 + 16
		char.Humanoid.Health = char.Humanoid.MaxHealth
		game.ReplicatedStorage.PlayerSpawned:FireClient(plr)
    end)
end)

nope, didn’t worked, you probably have to wait for LOVE.Value not for the GasterVoid, i might be wrong…

What kind of Instance type is GasterVoid and LOVE?

gaster is Folder, and LOVE is intValue that placed inside the folder

try

plr:WaitForChild("GasterVoid"):WaitForChild("LOVE")

i tried it, but didn’t worked, oh and changing my staterscript to this

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		plr:WaitForChild("GasterVoid")
	    char.Humanoid.MaxHealth = plr.GasterVoid.LOVE.Value*4 + 16
		char.Humanoid.Health = char.Humanoid.MaxHealth
		game.ReplicatedStorage.PlayerSpawned:FireClient(plr)
    end)
end)

or this

    game.Players.PlayerAdded:Connect(function(plr)
    	plr.CharacterAdded:Connect(function(char)
    		plr:WaitForChild("GasterVoid"):WaitForChild("LOVE")
    	    char.Humanoid.MaxHealth = plr.GasterVoid.LOVE.Value*4 + 16
    		char.Humanoid.Health = char.Humanoid.MaxHealth
    		game.ReplicatedStorage.PlayerSpawned:FireClient(plr)
        end)
    end)

just made another bug…
https://gyazo.com/c8fc63127367d0d56e539e955b3401be

game.Players.PlayerAdded:Connect(function(plr)
local love = plr:WaitForChild("GasterVoid"):WaitForChild("LOVE")
	plr.CharacterAdded:Connect(function(char)
	    char.Humanoid.MaxHealth = love.Value*4 + 16
		char.Humanoid.Health = char.Humanoid.MaxHealth
		game.ReplicatedStorage.PlayerSpawned:FireClient(plr)
    end)
end)

nope it still same result as the scripts before

maybe try this? i see no reason for it to break the script

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
	wait()
        plr:WaitForChild("GasterVoid"):WaitForChild("LOVE")
        char.Humanoid.MaxHealth = plr.GasterVoid.LOVE.Value*4 + 16
	char.Humanoid.Health = char.Humanoid.MaxHealth
	game.ReplicatedStorage.PlayerSpawned:FireClient(plr)
    end)
end)

yea didn’t broke the script but still starts with 20 hp ;-;