Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would i make a tool press repeatively when held down?

Asked by 4 years ago

So, i'm making a Singleplayer game where if you click it makes a part, But, I want you to be able to hold down MouseButton1 and parts to appear every 0.2 seconds, How would i fix that?

Here's my script

local lp = game.Players.LocalPlayer
local rs = game:GetService("RunService")
players = game:GetService("Players")



---------------------------------------------

local mouse = lp:GetMouse()

---------------------------------------------

script.Parent.Activated:Connect(function()
    local x = game.Workspace.Particle:Clone()
    x.Name = 'Parciclepart'
    x.Position = mouse.Hit.Position
    x.Parent = workspace
end)


---------------------------------------------

Thanks for reading, Help it appreciated.

1 answer

Log in to vote
1
Answered by
Is_Hunter 152
4 years ago
Edited 4 years ago
local Player = game:GetService("Players").LocalPlayer
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local mouse = Players:GetMouse()

script.Parent.Activated:Connect(function()
    local pressed = true
    while pressed  do -- repeat as long as the mouse button is pressed
        -- clone the part
        local x = workspace.Particle:Clone()
            x.Name = "ParticlePart"
            x.Position = mouse.Hit.Position
            x.Parent  = workspace

        -- wait 0.2s
        local start = tick()

        repeat
            RunService.Stepped:Wait()
        until tick() - start > 0.2

        -- check if the mouse button is pressed
        pressed  = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
    end
end)
1
Thank you! EllaTheFloofyFox 106 — 4y
Ad

Answer this question