OOP: Implentation Review

Not quite. It looks like you define the base object of a projectile in the server code and then make an object subclassing it in the module.

What you want is 1 modulescript = 1 object. You want to define the whole object in the module script and then utilize those objects in your server and local scripts.

Like this:

local projectile = {}
projectile.__index = projectile

function projectile.new(name, cframe)
	local newProjectile = {}
    setmetatable(newProjectile, projectile)
	
	newProjectile.Name = name
	newProjectile.Model = projectiles[name]:Clone()
	newProjectile.PrimaryPart = newProjectile.Model.PrimaryPart
	newProjectile.PrimaryPart.CFrame = cframe

    newProjectile.PrimaryPart.Touched:Connect(function(collision)
		print("An collision occured.")
		newProjectile:Explode(100)
	end)
	
	return newProjectile
end

function projectile:Explode(force)
	print("The projectile exploded with a force of "..tostring(force))
end

return projectile

Then you would simply utilize this modulescript (object) in server and local scripts. If you want to subclass it, do it in another modulescript that requires it, and then call that. I don’t recommend trying to create objects from within local and server scripts.

See this post for more info.

2 Likes