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

Remote Fires Despite not beind held down for enough time why?

The Button Would Continue To Register Being held down despite not being Held Down

local player = game:GetService("Players").LocalPlayer

local UseEvent = script.Parent:WaitForChild("Amaterasu")
local DeactivateEvent  =  script.Parent:WaitForChild("Stop")

local Key = "M" 
local startHoldingTime = tick() -- time when the player started holding. tick() is the current time
local expectedHoldTime = 3 -- How long you want the player to be hold the key for

local isBeingUsed = false

-- runs whenever the player presses a key
game:GetService("UserInputService").InputBegan:Connect(function(Input, gameProcessed)
    if gameProcessed then return end -- stops this event from firing if we're typing / roblox is using the keys in some way

    if Input.KeyCode == Enum.KeyCode[Key] then 
        startHoldingTime = tick()
    end
end)

--runs whenever the player stops pressing a key
game:GetService("UserInputService").InputEnded:Connect(function(Input, gameProcessed) 
    if gameProcessed then return end -- stops this event from firing if we're typing / roblox is using the keys in some way

    local timeHeldDown = tick() - startHoldingTime --how long the player held down the button in seconds

    if Input.KeyCode == Enum.KeyCode[Key] then
         print(player.Name..' holded '..Key..' for '.. timeHeldDown ..' second(s).')
        
        --if the player is using the mode already, fire the deactivate event
        if isBeingUsed and player.stats.Sage.Value == "Gates" then
            print("Firing the deactivate event!")
            DeactivateEvent:FireServer()
            isBeingUsed = false
        --if the player HASN'T used the mode yet, check if they held the key long enough to use the mode
        else
            if timeHeldDown >= expectedHoldTime and player.stats.Sage.Value == "Gates" then 
                print("Firing the used event!")
                isBeingUsed = true
                UseEvent:FireServer()
            else
                print("player didn't hold key for long enough to fire any events. ")
            end    
        end
        
    end
end)

1 answer

It may be due to you having tick already inside the variable, maybe try adding the tick in input began.

local player = game:GetService("Players").LocalPlayer

local UseEvent = script.Parent:WaitForChild("Amaterasu")
local DeactivateEvent  =  script.Parent:WaitForChild("Stop")

local Key = "M" 
local startHoldingTime = nil -- time when the player started holding. tick() is the current time
local expectedHoldTime = 3 -- How long you want the player to be hold the key for

local isBeingUsed = false

-- runs whenever the player presses a key
game:GetService("UserInputService").InputBegan:Connect(function(Input, gameProcessed)
    if gameProcessed then return end -- stops this event from firing if we're typing / roblox is using the keys in some way

    if Input.KeyCode == Enum.KeyCode[Key] then 
        startHoldingTime = tick()
    end
end)

--runs whenever the player stops pressing a key
game:GetService("UserInputService").InputEnded:Connect(function(Input, gameProcessed) 
    if gameProcessed then return end -- stops this event from firing if we're typing / roblox is using the keys in some way

    local timeHeldDown = tick() - startHoldingTime --how long the player held down the button in seconds

    if Input.KeyCode == Enum.KeyCode[Key] then
         print(player.Name..' holded '..Key..' for '.. timeHeldDown ..' second(s).')

        --if the player is using the mode already, fire the deactivate event
        if isBeingUsed and player.stats.Sage.Value == "Gates" then
            print("Firing the deactivate event!")
            DeactivateEvent:FireServer()
            isBeingUsed = false
        --if the player HASN'T used the mode yet, check if they held the key long enough to use the mode
        else
            if timeHeldDown >= expectedHoldTime and player.stats.Sage.Value == "Gates" then 
                print("Firing the used event!")
                isBeingUsed = true
                UseEvent:FireServer()
            else
                print("player didn't hold key for long enough to fire any events. ")
            end    
        end

    end
end)

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