InputEnded inside or outside InputBegan

So for my combat game, I want the client to detect when the player and lets go of F, using InputBegan and InputEnded.

And I was wondering, is it better to put my InputEnded event inside the InputBegan event or outside of it? Thanks.

Personally, I use InputEnded outside of my InputBegan event since it makes my code look neater.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function()
    ...
end)

UserInputService.InputEnded:Connect(function()
    ...
end)
3 Likes

Adding on to what @AustnBlox said, if you set up an event listener (let’s call this one listener2) inside of another event listener (let’s call this one listener1), everytime listener1 gets fired, you’d be creating more copies of listener2, which isn’t ideal, and probably interferes with performance.

It’s like writing this:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    -- Code here.

    -- Setting up unnecessary listeners instead of just creating one outside.
    Player.PlayerRemoving:Connect(function(plr)
        if plr ~= player then return end
        -- (plr == player) Code here.
    end)
end)

And most of the time when someone does this, they use the same code for every player. So they really could’ve just set the listener outside according to their logic. :face_holding_back_tears: