Attempt to perform arithmetic (sub) on nil and number

  1. What do you want to achieve?
    fix the bug below
  2. What is the issue?
    the code is giving me error
    Capture1
  3. What solutions have you tried so far?
    tried to search through dev forum but nothing solved my problem
local ts = game:GetService("TweenService")
local Rs = game:GetService("ReplicatedStorage")
local Ss = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local Base = {}

function Base.Setup(Health)  
	    Base.HealthUI = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Canvas").Upperinfo
		Base.CurrentHealth = Health		
		Base.MaxHealth = Health	
		Base.HealthUI["Base Health"].Title.Text = "Base Health: " .. (Base.MaxHealth) .. "/" .. (Base.CurrentHealth)  
	    print(Base.CurrentHealth)
end

function Base.DamageBase(damage)
	if damage then
		print(Base.CurrentHealth)
		Base.CurrentHealth -= damage --This line is giving the error
		local gui = Base.HealthUI["Base Health"]

		local tweeninfo = TweenInfo.new(
			0.5,
			Enum.EasingStyle.Exponential,
			Enum.EasingDirection.In,
			0,
			false,
			0
		)

		local percentage = Base.CurrentHealth/Base.MaxHealth


		local goal = {} 
		goal.Size = UDim2.new(percentage,0,0.25,0)

		local tween = ts:Create(gui.CurrentHealth,tweeninfo,goal)
		tween:Play()
		gui.Title.Text = ("Base Health: ") .. (Base.MaxHealth) .. ("/") .. (Base.CurrentHealth) 
		if Base.CurrentHealth <= 0 then
			gui.Title.Text = ("Base Health: OOF")
			Rs.Bindables.GameOver:Fire()
		end
	end
end	

Rs:WaitForChild("Bindables"):WaitForChild("DamageBase").Event:Connect(Base.DamageBase)	
return Base

can u show what line the error is

The error is caused as you are trying to substitute a number from nil. In this case, Base.CurrentHealth must be equal to nil for whatever reason. When you call Base.Setup, is there a chance you aren’t passing the Health, hence it being set to nil?

Note: Apologies, I intended to respond to the OP

The line for this error is indicated in the code given.

im talking about the specific line number

Base.Setup(100)

this how im calling it and its inside a local scripts

i already typed the error in the code, its in line 20

When you print Base.CurrentHealth, is it nill or the intended value? If not, are you setting it to nil in any other part of the code not provided here?

when i print the health here it prints out 100

but in the base.damagebase function it prints nil

for more easy to understand heres the script where im calling damage base function

local ServerStorage = game:GetService("ServerStorage")
local RS = game:GetService("ReplicatedStorage")
local Mob = {}

function Mob.Move(mob, map)
    
	local waypoints = map.waypoints
	for waypoint=1, #waypoints:GetChildren() do
			local Humanoid = mob:FindFirstChildOfClass("Humanoid",12)
		    Humanoid:MoveTo(waypoints[waypoint].Position)
		    Humanoid.MoveToFinished:Wait()
		    Humanoid:MoveTo(waypoints[waypoint].Position)
		    Humanoid.MoveToFinished:Wait()
	end     
	RS.Bindables.DamageBase:Fire(mob.Humanoid.Health/2)  -- this is where im calling the function
 	mob:Destroy()   
end

function Mob.Spawn(name, quantity, map)
	local mobExists = ServerStorage.Mobs:WaitForChild(name)
	if mobExists then
		for i=1, quantity do
			task.wait(0.5)
			local newMob = mobExists:Clone()
			newMob:PivotTo(map.Part.CFrame)
			newMob.Parent = workspace.Mobs
			newMob.HumanoidRootPart: SetNetworkOwner (nil)
			for i,object in ipairs(newMob:GetDescendants())do
				if object:IsA("BasePart")then
					object.CollisionGroup = "Mob"
				end
			end
 		        newMob:WaitForChild("Humanoid").Died:Connect(function()
				newMob.Parent = workspace
				wait (3)
				newMob:Destroy()
			end)
	coroutine.wrap(Mob.Move)(newMob, map)
		end
    else
		warn("Mob not found:", name)
	end
end


return Mob```

Are you calling Base.Setup before Base.DamageBase?

im calling both functions in different script and the damage base event is defiently after the base.setup function cuz base.damagebase function is fired when a specific dummy walks and reaches a point and when i call and print the the base.currenthealth

it print 100 directly when i test the game but

this one print the current health(nil) and after it prints nil its gives \the error when the dummy reaches that specific point