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
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
Clone.Touched:Connect(function(hitPart)
if hitPart and Players:GetPlayerFromCharacter(hitPart.Parent) == Players.LocalPlayer then
db = false
Clone:Destroy()
end
end)
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
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
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)