Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I fix this OOP script? It seems like whenever the method is called, it doesn't work.

I started currently learning OOP a few days ago and I have found a tutorial on the Dev Forum that explains it and after trying out the code snippet below, it seemed to work. However, a few days later when I tried to run the code again, it just outputs the error, "ReplicatedStorage.Car:17: attempt to perform arithmetic (add) on nil and number"

Since I am very new to learning OOP, I have no idea how to fix this. Can someone please explain to me why it doesn't work.

The main problem is coming from the :Boost method, which uses the self keyword and it seems like the error comes from whenever the method is called.

I am sure I follow it corrected, as I put the put a module script in ReplicatedStorage which held the constructor and the :Boost method, and I put a normal script(--main script) in the ServerScriptStorage.

I copied the code directly from the forum, so I didn't think it was any typo made by me.

--module script called Car in game.ReplicatedStorage
Car = {}
Car.__index = Car

function Car.new(position, driver, model)
    local newcar = {}
    setmetatable(newcar, Car)

    newcar.Position = position
    newcar.Driver = driver
    newcar.Model = model

    return newcar
end

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

return Car

--main script

Car = require(game.ReplicatedStorage.Car)

newcar = Car.new()
newcar:Boost()

1 answer

✓ Accepted answer

It's obviously because in function new you are creating Position, Driver and Model of the car but Speed is never defined, that way self.Speed is nil. Just define the default speed in the function like this

function Car.new(position, driver, model)
	local newcar = {}
	setmetatable(newcar, Car)
	
	newcar.Position = position
	newcar.Driver = driver
	newcar.Model = model
	newcar.Speed = 5 -- default speed for case

	return newcar
end

There is faster method tho in your case, rather than new indexing newcar for each property you would do this

function Car.new(position, driver, model)
	local newcar = {
		Position = position,
		Driver = driver,
		Model = model,
		Speed = 999,
	}
	setmetatable(newcar, Car)

	return newcar
end

This is almost a micro-optimisation but it optimizes it bit more than a micro optimization so i would use it, you don't have to tho, you still won't notice any differences.

You should also use += instead of indexing self.Speed twice, here is example of how it works

local Speed = 5

Speed += 5 -- Speed is now 10
Speed -= 5 -- Speed is now 5 again
Speed /= 2 -- Speed is now 2.5
Speed *= 2 -- Speed is now 5
Speed *= 2 -- Speed is now 10

-- for you would be
self.Speed += 5

This works for all operators [.., +, -, /, *, %, ^].

2 comments

0

You can use += now? I thought they didn't include this in Lua?

This discussion is preserved as it was. New answers, comments, and votes are closed.