Im writing a module to handle the client side placement and i will write a script on the server. Im getting this error. attempt to call method 'Place' (a nil value) Module:
local Placer = {} local runservice = game:GetService("RunService") local player = game:GetService("Players").LocalPlayer local mouse = player:GetMouse() function Placer:Place(model, base) self._running = true self._model = model self._base = base end function Placer:Cancel() self._running = false self._model = nil self._base = nil self._roundedpos = nil; end function round(num,multiple) if num%multiple==0 then return num elseif num%multiple>multiple/2 then i = 0 repeat i = i+1 until (i*multiple>num) return i*multiple elseif num%multiple<((multiple/2)+.5) then i = 0 repeat i = i+1 until (i*multiple>num) return ((i*multiple)-multiple) end end function roundVector3(vector3, roundby) local newvector3 = Vector3.new(round(vector3.x, roundby),round(vector3.y, roundby),round(vector3.z, roundby)) return newvector3 end function contains(lit, object) for i,v in pairs(lit) do if v == object then return true end end return false end function Placer:Update() if self._running then local previous = self._roundedpos or roundVector3(Vector3.new(self._base.Position.X, self._base.Position.Y+ (self._model.PrimaryPart.Size.Y / 2) + (self._base.Size.Y / 2), self._base.Position.Z), 2) local new = roundVector3(mouse.Hit.p) self._model:SetPrimaryPartCFrame(CFrame.new(new)) if contains(self._model.PrimaryPart:GetTouchingParts(), self._base) then self._roundedpos = new else self._model:SetPrimaryPartCFrame(CFrame.new(previous)) end end end function Placer.new() local self = setmetatable({_running = false; _model = nil; _base = nil; _roundedpos = nil; }, Placer) return self end return Placer
then the local:
local runservice = game:GetService("RunService") local placemodule = require(script.ModuleScript) local placer = placemodule.new() placer:Place(workspace.Test, workspace.Base) runservice.RenderStepped:Connect(function() placer:Update() end)
Thank you in advance. Notice: I want to keep it as a module script instead of changing it to a normal localscript.
The error is happening on line 7 of the local script where you call Place
as a method of placer
. The problem is that Place
doesn't exist in the table and the metatable assigned to your instantiation in the initializer is incorrect.
Replace lines 121 - 129 in your module script with this: ```lua local self = setmetatable({_running = false;
_model = nil;
_base = nil;
_roundedpos = nil;
}, {__index = Placer}) ```
Now when you call placer:Place(workspace.Test, workspace.Base)
, the interpreter will look into placer
with the key Place
. It will realize that it doesn't exist in the table, so it will look into the __index
property of placer
's metatable. Before the fix, it would have stopped here and returned nil
because __index
wouldn't have had a value. Now that it has a value, it finds the appropriate method in the Placer
table and correctly calls it with the appropriate parameters.