Want to create an Instance.new("Part") loop without crashing the server

Amateur scripter here. I want to add a loop function where it places down new parts every time I hold click with a wait interval of (0.01) seconds.

I am currently trying to add a loop script, but I seem to be confused which type of loop script and where I should put the loop script in my code.

I’ve tried to find solutions on the forum, but it seems like there is none to me. I will show a video example of what I want to do below. (ps. I was using an auto clicker)

VIDEO

Below was the code I was trying to do.

CODE

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Up:Connect(function()
	while true do
		wait(1)
	local part = Instance.new("Part")
	part.Size = Vector3.new(10,10,10)
	part.Shape = Enum.PartType.Cylinder
	part.Anchored = false
	part.Material = "Plastic"
	part.BrickColor = BrickColor.new("Bright blue")
	part.Position = mouse.Hit.Position
	part.Parent = workspace
	wait(5)
	part.CanCollide = false
	wait(3)
		part:Destroy()
		end
	end)

Thanks for anyone who gives feedback!

Not sure how you want the functionality, but if you want something like repeatedly making a part as along as the mouse button is down, you can spawn a function that does the loop upon the mouse button down, and when the button goes up, stop the function. You could do this by creating an instance that the loop checks for in which it will terminate when it sees it.

2 Likes
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local summoning = false
function startSummon()
    while summoning and task.wait(1) do
        local part = Instance.new("Part")
	    part.Size = Vector3.new(10,10,10)
	    part.Shape = Enum.PartType.Cylinder
	    part.Anchored = false
	    part.Material = "Plastic"
	    part.BrickColor = BrickColor.new("Bright blue")
	    part.Position = mouse.Hit.Position
	    part.Parent = workspace
        task.delay(3, function() part:Destroy() end)
    end
end

mouse.Button1Down:Connect(function()
	summoning = true
    startSummon()
end)

mouse.Button1Up:Connect(function()
    summoning = false
end)
1 Like

Here should be a better version of your script

local Debris = game:GetService("Debris") -- A "Better" version of Destroy
local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local Hold = false

DelayTime = .1

Mouse.Button1Down:Connect(function()
	Hold = true -- Enables Hold
end)

Mouse.Button1Up:Connect(function()
	Hold = false -- Disables Hold
end)

while wait(DelayTime) do -- Constantly checks that Status of "Hold"
	if Hold == true then -- Checks if Hold Enabled
		task.spawn(function() -- Fires without Distruption
		local Part = Instance.new("Part", workspace)
		Part.Material = Enum.Material.Plastic
		Part.BrickColor = BrickColor.new("Bright blue")
        Part.Position = Mouse.Hit.p
		Debris:AddItem(Part, 8)
		wait(5)
		Part.CanCollide = false
		end)
	end
end
1 Like

This worked for me! I didn’t realize I had to make a local variable which equals false, and I also didn’t know there was a task script I could have done. New things to learn, huh.

1 Like

What’s the difference between destroy and debris?

Here is Documentation:

1 Like

It’s the same.

Debris is a service that performs Destroy after a countdown. Maybe it’s a bit cleaner than my task.delay method.

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