Why is not my model being placed when I click?
So.. I am developing / scripting a placement system for models, and what I want it to do, is that the model can only be placed if the mouse / preview of the model is on the "Cells" I created.
If you do not understand what I am trying to accomplish, then think of the placement system in Miner's Haven, I want the model only to get placed whenever the preview / mouse is over the "cells".
I have tried the following(See code below). I know for certain that I am indeed doing something wrong, but that is why I seek help here.
Please note: I am not asking for anyone to create a script for me, all I really want to know is what I am doing wrong, and what I could do to fix what I am doing wrong.
Any help is greatly appreciated!
Coding for my placement system:
local rs = game:WaitForChild("ReplicatedStorage")
local model = rs:WaitForChild("ModelToPlace")
local cells = game.Workspace.Cells:GetChildren() --The "Cells" the model CAN / SHOULD be placed on, the cells has a "Studs" surface
-- There is a model called "Cells" with all the cells in them
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService("RunService")
local cloneModel = model:Clone()
cloneModel.Parent = game.Workspace
local modelCFrame = cloneModel:GetModelCFrame()
mouse.TargetFilter = cloneModel
function roundAToB(a,b)
return(math.floor((a/b)+0.5)*b)
end
runService.RenderStepped:connect(function()
local hit = mouse.Hit.p
--CloneModel:SetPrimaryPartCFrame(CFrame.new(hit)*CFrame.new(0,modelCFrame.Y/2,0))
cloneModel:SetPrimaryPartCFrame(CFrame.new(roundAToB(hit.X,1),1.5,roundAToB(hit.Z,1)) * CFrame.Angles(0,0,math.rad(0)))
end)
mouse.Button1Down:connect(function()
if mouse.TargetSurface == cells.TopSurface then --This should in my head check if the mouse / preview is hovering the cells, but it does not work. Why?
local new = cloneModel:Clone() --And nothing gets placed anywhere because of what it says in the above comment^
new.Parent = cloneModel.Parent
end
end)

1 comment
I am sorry to inform you that your answer has not seemed to work.