Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to perform arithmetic (div) on nil. How to fix?

I'm a small game creator i have a problem with Base Health in my TD game how to fix it? problem is: ServerScriptService.Main.Base:20: attempt to perform arithmetic (div) on nil Help pls

local serverstorage = game:GetService("ServerStorage")

local Bindables = serverstorage:WaitForChild("Bindables")

local UpdateHealthEvent = Bindables:WaitForChild("UpdateBaseHealth")

local GameOverEvent = Bindables:WaitForChild("GameOver")

local Base = {}

function Base.Setup(map, health)
	Base.Model = workspace.map.Base
	Base.CurrentHealth = health
	Base.MaxHealth = health
	Base.UpdateHealth()
end

function Base.UpdateHealth(damage)
	if damage then
		Base.CurrentHealth -= damage
	end
	
	local gui = workspace.map.Base.HealthGui
	local precent = Base.CurrentHealth / Base.MaxHealth
	
	gui.CurrentHealth.Size = UDim2.new(precent, 0, 0.5, 0)
	
	if Base.CurrentHealth <= 0  then
		GameOverEvent:Fire()
		print("gameover")
		gui.Title.Text = "Base:Destroyed "
	end
	
	gui.Title.Text = "Base: " .. Base.CurrentHealth .. "/" .. Base.MaxHealth
	
	
end

UpdateHealthEvent.Event:Connect(Base.UpdateHealth())

return Base
~~~~~~~~~~~~~~~~~i

1 comment

0

please edit your post and make the ~~~ on a differrent line so its all in a texbox, and we can see what line the error is on. Also, can I help you with your tds game?

1 answer

"attempt to perform arithmetic (div) on nil" means you're trying to use the / (division) operator on something that is nil (has no value), and the only sign of division I see is on "Base.CurrentHealth / Base.MaxHealth".

Looking at your code you have a function Base.Setup that assigns values to both Base.CurrentHealth and Base.MaxHealth, but I don't see it mentioned anywhere in the code you specified, possibly the reason why your properties are nil.

This discussion is preserved as it was. New answers, comments, and votes are closed.