I have the following placement script, where a player clicks and an object is cloned. Currently the player is able to click and place objects without limit.
I am trying to put a while loop to make it only able to be placed once, then it stops 'previewing' the object.
The preview is when the object follows the mouse around and only when clicked, the object is cloned into the workspace.
local rs = game:WaitForChild("ReplicatedStorage")
local model = rs:WaitForChild("Grass")
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
local placed = false
while placed == false do
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,4),1.5,roundAToB(hit.Z,4)) * CFrame.Angles(0,0,math.rad(0)))
for i,v in pairs(cloneModel:GetChildren()) do
if v:IsA('BasePart') then
v.Transparency = 0.6
v.CanCollide = false
end
end
end)
mouse.Button1Down:connect(function()
local new = cloneModel:clone()
new.Parent = workspace
for i,v in pairs(new:GetChildren()) do
if v:IsA('BasePart') then
v.Transparency = 0
v.CanCollide = true
end
end
end)
placed = true
end
Currently the while loop does not work as the player is still able to place objects without limit.

1 comment
This works perfectly, thanks!