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

Is it possible to loop an on keypress script and stop when you let go of the keypress?

Asked by 5 years ago
Edited 5 years ago

Basically this is just a test teleport script and I am trying to figure out how to loop it while the V key is held down. When you press V it teleports you to two different locations as you can see below. Would it be possible to keep teleporting back and forth (loop) and stop when you let go of the V key? I can't seem to figure this out. Your help is very much appreciated!

local client = game:GetService("Players").LocalPlayer
game:GetService("UserInputService").InputBegan:Connect(function(input, gpe)
    if gpe then return end

    if input.KeyCode == Enum.KeyCode.V then
        client.Character:SetPrimaryPartCFrame(CFrame.new(275,3.94,347.568))
        wait(0.1)
        client.Character:SetPrimaryPartCFrame(CFrame.new(275,5.94,347.568))
    end
end)
0
did you try a while loop? not certain but would be my first thought DinozCreates 1070 — 5y
0
Have you tried using tweening methods? xPolarium 1388 — 5y
0
It is possible I formatted it wrong but adding While did not work for me :/ PieBoots 16 — 5y
0
@xPolarium I have not tried. I am not sure how to incorporate that into the script. I was under the impression that tweening was only for animations. PieBoots 16 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

here u go

local pressed = false
local client = game:GetService("Players").LocalPlayer
game:GetService("UserInputService").InputBegan:connect(function(input, gpe)
    if gpe then return end

    if input.KeyCode == Enum.KeyCode.V then
    pressed = true
    while pressed == true do
    client.Character:SetPrimaryPartCFrame(CFrame.new(275,3.94,347.568))
        wait(0.1)
        client.Character:SetPrimaryPartCFrame(CFrame.new(275,5.94,347.568))
        wait(0.1)
        end
    end
end)

game:GetService("UserInputService").InputEnded:connect(function(input, gpe)
    if gpe then return end

    if input.KeyCode == Enum.KeyCode.V then
    pressed = false
    end
end)
0
Wow thanks a ton, this works perfectly. It seems to be way more complicated than initially thought. You are awesome, thanks again. PieBoots 16 — 5y
Ad

Answer this question