How to get code to work with 2 different methods being used

Kinda hard to explain, but here goes nothing. So what I have is a module, RenderPosition, which I use to set the position of a part based on the mouses position. I have 2 modules, one for placing walls, one for placing objects. Now, starting with walls, the RenderPosition works well for placing the walls. However, with object placing, since it is a model instead of a part means this RenderPosition doesn’t work.

RenderPosition Module

return function(playersPlot, mouseP, object, lowerX, upperX, lowerZ, upperZ)
	local mouseClampedP = clampMouse(mouseP, lowerX, upperX, lowerZ, upperZ)

	object.CFrame = CFrame.new(round(mouseClampedP + Vector3.new(0, object.Size.X/2, 0), gridSize, playersPlot.Base.Position.Y + (object.Size.X/2) + 0.05))*CFrame.Angles(0, 0, math.rad(90))
end

DrawWall Module
pole1 is a single part

renderStepped = runService.RenderStepped:Connect(function()
	renderPosition(playersPlot, mouse.Hit.p, pole1, lowerX, upperX, lowerZ, upperZ)
end)

PlaceObject Module
item is a model, with a PrimaryPart being like it’s hitbox

renderStepped = runService.RenderStepped:Connect(function()
	renderPosition(playersPlot, mouse.Hit.p, item, lowerX, upperX, lowerZ, upperZ)
end)

I really don’t wanna have 2 seperate modules to basically do the same thing. So is there anyway to get this is work for both modules. To also note, I can’t set a PrimaryPart for the wall one, as it would ruin the functionality, so something would have to change in the object placement module, but Idk what

1 Like

Consolidate the modules to support different types. One method of doing this is adding an argument that specifies whether an object is a single part or a model, though this can be done in-script.

RenderPositionModule:

return function(playersPlot, mouseP, object, lowerX, upperX, lowerZ, upperZ)
	local mouseClampedP = clampMouse(mouseP, lowerX, upperX, lowerZ, upperZ)

	object.CFrame = CFrame.new(round(mouseClampedP + Vector3.new(0, object.Size.X/2, 0), gridSize, playersPlot.Base.Position.Y + (object.Size.X/2) + 0.05))*CFrame.Angles(0, 0, math.rad(90))
end

ObjectPlaceModule:

local mode = (object:IsA("Model") and "Model" or object:IsA("BasePart") and "BasePart" or "Reject")

if not mode == "Reject" then
    renderStepped = runService.RenderStepped:Connect(function ()
        if mode == "Model" then
        -- Model
        elseif mode == "BasePart" then
        -- Part
        end
    end)
end

Probably not the most elegant of solutions to suggest if you care a lot about how your code looks, but it certainly seems like it’d handle the job well enough. This is as much as I can offer with what’s been provided as well.

1 Like

How would I position the model tho? If it’s

if mode == 'Model' then

but still run the RenderPosition module, it wont work, as RenderPosition relies on a singular part instead of a model.

And sending the PrimaryPart as that part would not work as editing the CFrame of a PrimaryPart only affects that parts position. I’d have to use :SetPrimaryPartPosition, but I can’t do that either, as if it’s only a single part it doesnt have a PrimaryPart

In terms of the model, you can move a transparent part and make the model’s parts’ CFrames offset the moved part.

Well I currently do have a transparent part being the PrimaryPart, but as I said in my previous point, change said CFrame of PrimaryPart doesn’t work, and not sure how to position the parts within the model based on the PrimaryParts position without using SetPrimaryPartCFrame()

It would probably be best if you just put the walls in a Model. It’ll allow you to add complex designs in the future and solves your issue. Can you explain how it’ll

?

The walls are technically inside a model, but are being passed thru as their individual part. I can’t use a PrimaryPart to decide the position of the wall, as wall placement works like how the sims does wall placement. So a pole follows the mouse, click and drag and another pole is added. So having a PrimaryPart would not really work in that case