Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 1 year 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 = 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

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? Kingu_Criminal 205 — 1y

1 answer

Log in to vote
0
Answered by 1 year 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.

Ad

Answer this question