Attempt to call a nil value?

im trying to learn oop, but when i call newcar:Boost(), it errors this:

  14:01:15.547  ServerScriptService.Script:23: attempt to call a nil value  -  Server - Script:23
  14:01:15.548  Stack Begin  -  Studio
  14:01:15.548  Script 'ServerScriptService.Script', Line 23  -  Studio - Script:23
  14:01:15.548  Stack End  -  Studio

here is the full script:


car = {}
car._index = car

function car.new(pos,model,driver,worldpos,speed)
	local newcar = {}
	setmetatable(newcar,car)
	
	newcar.Position = pos
	newcar.Model = model
	newcar.Driver = driver
	newcar.WorldPosition = worldpos
	newcar.Speed = speed
	
	return newcar
end

function car:Boost()
	self.Speed = self.Speed + 5
end

local newcar = car.new(3,game.ReplicatedStorage.Car,"userni3",Vector3.new(10,0,10),5)
newcar:Boost()
print(newcar)
1 Like

Make sure to require the module script.

i dont have any module script tho

Also make sure to put a car model in replicated storage and use wait for child to make sure it exist if on client.

why is the function called car:Boost and not just Boost

that’s unrelated to the question on the original post

because im trying to learn oop

object oriented programming?–

yes, thats what im trying to learn

Nah, it’s erroring because it’s trying to access the car variable in replicated storage but it doesn’t exist hence a nil error.

Just my bet on it.

it does exist in replicatedstorage

Oh whoops found the issue, it’s a typo with the meta table.

There needs to be 2 underscores

car.__index = car

Then it gets the fancy highlighting.

And it does the metatable redirection from

{--newcar table, doesn't have Boost function without the help of meta table
                    ["Driver"] = "userni3",
                    ["Model"] = Car,
                    ["Position"] = 3,
                    ["Speed"] = 10,
                    ["WorldPosition"] = 10, 0, 10
                 }  -  

--the meta table tells it to index from the car table
 {--car table printed out boost function is accessed here
                    ["Boost"] = "function",
                    ["__index"] = "*** cycle table reference detected ***",
                    ["new"] = "function"
                 }  -  Server - Script:24
1 Like

yeah you’re right -------------

roblox studio autocorrect put 1 underscore instead of two, im dumb

1 Like

if that was the case then, “attempt to call a nil value” wouldn’t have been shown at all

if you look at the code in :Boost() it doesn’t even mess with self.Model, it messes with self.Speed

Yep sorry, that’s a mistake. I was guessing at all the possible nil error possibilities and mixed up attempt to index nil vs attempt to call a nil value (really common problem on #help-and-feedback:scripting-support ).