Need help with my while true do script

I’m currently trying to make a minigame where, using math.random, cylinders will appear in random spots, and you must touch them to make another one spawn, and it should keep going. However, after I touch the cylinder to delete it, nothing else happens. So, I’m now stuck on how to repeat the process.

> local db = true
> 
> while true do
> 	if db == true then	
> 	local RandomNumber = math.random(-27,27)
> 	local RandomNumber2 = math.random(-27,27)
> 	print(RandomNumber)
> 	print(RandomNumber2)
> 
> 	local Clone = game.Workspace.PartToClone:Clone()
> 	Clone.Parent = game.Workspace
> 	print(RandomNumber)
> 	
> 	Clone.Name = ('Clone')
> 	Clone.Shape = ('Cylinder')
> 	Clone.Position += Vector3.new(0,1,0)
> 	Clone.Size = Vector3.new(20,6,6)
> 	Clone.Rotation = Vector3.new(0,0,90)
> 	Clone.Position += Vector3.new(0,9,0)
> 	Clone.BrickColor = BrickColor.new "Gold"
> 	Clone.Transparency = 0.6
> 	Clone.CanCollide = false
> 	Clone.Position += Vector3.new(RandomNumber, 0, RandomNumber2)
> 	wait(nil)
> 	db = false
> 	Clone.Touched:Connect(function()
> 		Clone:Destroy()	
> 			db = true
> 		end)	
> 	end
> end

Try this:

local Players = game:GetService("Players")

local db = false
while true do
    if db then continue end

 	local RandomNumber = math.random(-27,27)
 	local RandomNumber2 = math.random(-27,27)
 
 	local Clone = game.Workspace.PartToClone:Clone() 	
 	Clone.Name = ('Clone')
 	Clone.Shape = ('Cylinder')
 	Clone.Position += Vector3.new(0,1,0)
 	Clone.Size = Vector3.new(20,6,6)
 	Clone.Rotation = Vector3.new(0,0,90)
 	Clone.Position += Vector3.new(0,9,0)
 	Clone.BrickColor = BrickColor.new "Gold"
    Clone.Transparency = 0.6
	Clone.CanCollide = false
 	Clone.Position += Vector3.new(RandomNumber, 0, RandomNumber2)
 	Clone.Parent = game.Workspace
 	db = true
 	Clone.Touched:Connect(function(hitPart)
        if hitPart and Players:GetPlayerFromCharacter(hitPart.Parent) == Players.LocalPlayer then
 		Clone:Destroy()	
 			db = false
 		end)	
 	end
end
1 Like

Now the part won’t delete whenever I touch it.

Oh wait, nevermind, works only as normal player. But it still doesn’t spawn a new part in.

Clone.Touched:Connect(function(hitPart)
    if hitPart and Players:GetPlayerFromCharacter(hitPart.Parent) == Players.LocalPlayer then
		db = false
	    Clone:Destroy()	
 	end	
end)
1 Like

Now it deletes correctly, but it still has trouble creating a new part when looping back through the script.

Store the part in ReplicatedStorage instead of workspace. Also could you show the code up until now?

Same error.

local Players = game:GetService("Players")

local db = false
while true do
	if db then continue end

	local RandomNumber = math.random(-27,27)
	local RandomNumber2 = math.random(-27,27)

	local Clone = game.Workspace.PartToClone:Clone() 
	Clone.Parent = game.ReplicatedStorage	
	Clone.Name = ('Clone')
	Clone.Shape = ('Cylinder')
	Clone.Position += Vector3.new(0,1,0)
	Clone.Size = Vector3.new(20,6,6)
	Clone.Rotation = Vector3.new(0,0,90)
	Clone.Position += Vector3.new(0,9,0)
	Clone.BrickColor = BrickColor.new "Gold"
	Clone.Transparency = 0.6
	Clone.CanCollide = false
	Clone.Position += Vector3.new(RandomNumber, 0, RandomNumber2)
	Clone.Parent = game.Workspace
	db = true
	Clone.Touched:Connect(function(hitPart)
		if hitPart and Players:GetPlayerFromCharacter(hitPart.Parent) == Players.LocalPlayer then
			Clone:Destroy()	
			db = false
		end	
	end)
end

Set db to false before destroying clone

My guess is that the part falls down because it has CanCollide to false, maybe try anchoring the part?

Doesn’t seem to make any changes.

I added in

	Clone.Anchored = true

That doesn’t seem to work either.

Hello @SentorAmery250, is this what you were after?

local db = true

-- You don't actually have to do while true do, you can embed the wait in here if it'll always run.
-- Additionally, you don't need to pass it nil, if there's no parameter, the default is nil.
while task.wait() do
	
	-- The debounce is fine.
	if db then	
		
		-- This stuff is okay too.
		local RandomNumber = math.random(-27,27)
		local RandomNumber2 = math.random(-27,27)
		print(RandomNumber)
		print(RandomNumber2)

		local Clone = game.Workspace.PartToClone:Clone()
		Clone.Parent = game.Workspace
		print(RandomNumber)

		Clone.Name = ('Clone')
		Clone.Shape = ('Cylinder')
		Clone.Position += Vector3.new(0,1,0)
		Clone.Size = Vector3.new(20,6,6)
		Clone.Rotation = Vector3.new(0,0,90)
		Clone.Position += Vector3.new(0,9,0)
		Clone.BrickColor = BrickColor.new "Gold"
		Clone.Transparency = 0.6
		Clone.CanCollide = false
		Clone.Position += Vector3.new(RandomNumber, 0, RandomNumber2)
		
		-- Added an anchored in here since now the part isn't always constantly changing positions.
		Clone.Anchored = true
		
		db = false
		
		Clone.Touched:Connect(function(hit)
			
			-- We need to check if the parent has a Humanoid (a character) but for a sanity check, get the player object from character.
			
			-- If it's not just anyone that can touch it, then put all this script in a LocalScript instead and replace the below check with:
				-- hit.Parent:FindFirstChildWhichIsA("Humanoid") and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) == game:GetService("Players").LocalPlayer
			if hit.Parent:FindFirstChildWhichIsA("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then
				Clone:Destroy()	
				db = true
			end
		end)	
	end
end

Sorry for the late response, but this worked! Thank you for the additional comments on the script too.

1 Like

Not a problem, additionally, it might be worth disposing of the connected function of the .Touched event you have, all you would need to do is set a blank local variable just above it, then set it TO the clone.Touched:Connect(). Within the touch check where we destroy, you’d just need to use :Disconnect() on the variable you create and assign the connection to, if that makes sense?

Example:

local touchConnection
touchConnection = <PART>.Touched:Connect(function(hit)
    -- If condition here for the touch condition etc...
    if ... then
        touchConnection:Disconnect()
        touchConnection = nil
    end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.