Log in to vote

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

Asked by 12 days ago

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 = Base.CurrentHealth = health Base.MaxHealth = health Base.UpdateHealth() end

function Base.UpdateHealth(damage) if damage then Base.CurrentHealth -= damage end

local gui =
local precent = Base.CurrentHealth / Base.MaxHealth

gui.CurrentHealth.Size =, 0, 0.5, 0)

if Base.CurrentHealth <= 0  then
    gui.Title.Text = "Base:Destroyed "

gui.Title.Text = "Base: " .. Base.CurrentHealth .. "/" .. Base.MaxHealth



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

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? Kingu_Criminal 120 — 11d

1 answer

Log in to vote
Answered by 11 days ago

"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.


Answer this question