I'd like to build off of what zamd suggested. In general, I agree with his usage of InputEnded, however, I'd even suggest using an actual BoolValue instance in your game to determine the value of holding. This way, you can run .Changed on it! I use this method a lot for issues like yours. Here's an example.
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local IsHolding = Instance.new("BoolValue")
IsHolding.Name = "IsHolding"
IsHolding.Parent = Player
UIS.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.R then
IsHolding.Value = true
end
end)
UIS.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.R then
IsHolding.Value = false
end
end)
IsHolding.Changed:Connect(function()
if IsHolding.Value == true then
else
end
end)
6 comments
I'd recommend using
.Changedinstead of awhileloop"holding" isn't an object so I believe that is impossible.
My point exactly. See my answer below
But the while loop suits better, because the .Changed event will only run ONCE, and the while loop will run COUNTINOUSLY when the value is true.
That's exactly the problem. It's a waste of resources. There's no need to make it run continuously, when the action will have already been completed with the first iteration. Thus, running it in
.Changedimproves performanceTitle of the question: "How would you a key that you can hold?" changed will only run ONCE. And "improves performance" doesn't make any sense, as the loop will only play when necessary (when the value is true, aka. holding the key), NOT ALL THE TIME. That means it won't cause any lag if handled properly. I use a loop in one of my games to detect every second what parts are in 60 region3's (by your