Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[SOLVED] My while true loop is not taking action?

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 answer

✓ Accepted answer

Your while loop does nothing, as you exit from the loop immediately. What you really want to do is to disconnect the events you have set up after the user has clicked and successfully built something. You should have a function to disconnect these connections so that you can also cancel it (in case they no longer want to place it - though you have to set up those controls yourself, if you want to).

local con1, con2
function DisconnectConnections()
	con1:Disconnect()
	con2:Disconnect()
	--You might want to destroy or deparent the clone model here, too
end
con1 = runService.RenderStepped:connect(function()
	--etc
end)
con2 = mouse.Button1Down:connect(function()
	--etc. Call DisconnectConnections() when they've placed something
end)

1 comment

0

This works perfectly, thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.