Custom health system bug

wait(1)
startingheartx = 0.004
startinghearty = 0.011
Lighting = game.Lighting
hearts = {}

function NewHeart(httype)
	local prevheart = nil
	if httype == "Empty_Half" then
		if #hearts == 0 then
			local Heart = Lighting.Hearts.Empty_Half:Clone()
			Heart.Name = "Empty_Half_1"
			print(Heart.Name)
		Heart.Parent = script.Parent
		table.insert(hearts, 1, "Empty_Half")
		else
			if hearts[1] == "Empty_Half" then
				local counter = tostring(#hearts)
				local lastheart = script.Parent:FindFirstChild("Empty_Half_"..counter)
				local lastposition = lastheart.Position
				lastheart:Remove()
				local Heart = Lighting.Hearts.Empty_Full:Clone()
				Heart.Position = lastposition
				Heart.Parent = script.Parent
				hearts[#hearts] = "Empty_Full"
				table.remove(hearts, 2)
			else
				local Heart = Lighting.Hearts.Empty_Half:Clone()
			Heart.Position = UDim2.new(0.004 + (#hearts * 0.03), 0, 0.011, 0)
			Heart.Name = Heart.Name.."_".. tostring(#hearts)
		Heart.Parent = script.Parent
		table.insert(hearts, 1, "Empty_Half")
			end
		end
	end
end

function StartItUp()
	local heartcount = 0
	repeat
		print("try "..heartcount)
		NewHeart("Empty_Half")
		heartcount = heartcount + 1
		wait()
	until heartcount == 6
end

StartItUp()

This is all the lonely 48 lines of code I have written for a different health system. It makes halves and converts two halves into a full, then moves other halves to the side. However, i get a little error that I’ve given up on fixing on “try 3”.

  23:47:47.081 - Players.hboogy101.PlayerGui.ScreenGui.LocalScript:20: attempt to index local 'lastheart' (a nil value)
23:47:47.082 - Stack Begin
23:47:47.083 - Script 'Players.hboogy101.PlayerGui.ScreenGui.LocalScript', Line 20 - global NewHeart
23:47:47.084 - Script 'Players.hboogy101.PlayerGui.ScreenGui.LocalScript', Line 42 - global StartItUp
23:47:47.085 - Script 'Players.hboogy101.PlayerGui.ScreenGui.LocalScript', Line 48
23:47:47.085 - Stack End

I’m able to make one full heart and then a half, but then it gives me this error. Can any of you tell me how to fix my messy code?

1 Like

Hold on, i’ll see if I can see an issue

3 Likes

What part is this line addressing?
``
local lastheart = script.Parent:FindFirstChild("Empty_Half_"…counter)`

1 Like

It tries to find the last half-heart used with the table, the one that would be in script.parent

1 Like

Could I get a picture of that?
Right now it seems as if “empty_half_” is a part if i’m correct.

1 Like

Check to make sure “lastheart” is non-nil.

if lasheart ~= nil then
–fiddly-doo
end

You’re getting the exception because the script cannot find a child under the name (“Empty_Half_”…counter). I am aware that you’re using concatenation in this script, but there is no such object child to the script’s parent.

3 Likes

I’ve got the answer.

1 Like

Under my own code, I was telling it to look for something that didn’t exist and all I had to do was use the explorer. Thank you.

1 Like

Let’s hope this will be the last time the both of us get this exception, because I can’t tell you how many times I make stupid mistakes like this.

All in being human! :slightly_smiling_face: :+1:

3 Likes