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