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)
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.
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
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.