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

How do i stop UserInputService from stacking up my while loop?

Asked by
Gunt_r 31
4 years ago
Edited 4 years ago

I am trying to make a "staircase" tool that essentially allows the player to traverse anywhere in the sky. it basically puts blocks under the players feet continuously whenever the input is held down. That works fine, however, the issue i'm having is that the staircasing gets exponentially increased whenever i press another key due to the nature of the while loop. (The while loop persists and infinitely doubles up on itself everytime i give it new inputs).

I dont want that. How do i stop it from exponentially increasing and keep it at a constant generation speed whilst still allowing movement. In other words, how do i stop the while loop from stacking up on itself.

Thanks

local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")


function holdTool(input,wasProcessed)
    local flag = true
    --if UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) == true and flag then
        while UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) == true and flag do
            print(input)
            print(input.UserInputType)
            local bodyPosition = game:GetService("Players").LocalPlayer.Character.HumanoidRootPart.Position
            local flyingBit =  Instance.new("Part",workspace.Flying_Pieces)
            flyingBit.Size = Vector3.new(5,1,5)
            flyingBit.CFrame = CFrame.new(bodyPosition.X,bodyPosition.Y-4,bodyPosition.Z)
            wait(0.2)
        --if UserInputService:GetKeysPressed then
        --end
    end

    print("end")
end


function doNothing()

end

if Tool.Equipped then
    UserInputService.InputBegan:Connect(holdTool)
    UserInputService.InputEnded:Connect(doNothing)
end

Gif of the problem: https://gyazo.com/9132c0b102dd4d1d07f1d2f90c8347a8

1 answer

Log in to vote
0
Answered by 4 years ago

You can use a debounce mechanism that simply switches from true to false to true and is a condition in the while loop. You can toggle it in the holdTool and then the doNothing functions like this:

local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
loca keyHeld = false --Initiate value

function holdTool(input,wasProcessed)
    keyHeld = true -- Toggle true to begin
    local flag = true
    --if UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) == true and flag then
        while UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) == true and flag and keyHeld do -- If keyHeld == false, the loop stops
            print(input)
            print(input.UserInputType)
            local bodyPosition = game:GetService("Players").LocalPlayer.Character.HumanoidRootPart.Position
            local flyingBit =  Instance.new("Part",workspace.Flying_Pieces)
            flyingBit.Size = Vector3.new(5,1,5)
            flyingBit.CFrame = CFrame.new(bodyPosition.X,bodyPosition.Y-4,bodyPosition.Z)
            wait(0.2)
        --if UserInputService:GetKeysPressed then
        --end
    end

    print("end")
end


function doNothing()
    keyHeld = false -- Toggle false to stop
end

if Tool.Equipped then
    UserInputService.InputBegan:Connect(holdTool)
    UserInputService.InputEnded:Connect(doNothing)
end
Ad

Answer this question