Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

4 comments

0

did you try a while loop? not certain but would be my first thought

0

Have you tried using tweening methods?

0

It is possible I formatted it wrong but adding While did not work for me :/

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.

1 answer

✓ Accepted answer

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)

1 comment

0

Wow thanks a ton, this works perfectly. It seems to be way more complicated than initially thought. You are awesome, thanks again.

This discussion is preserved as it was. New answers, comments, and votes are closed.