UserInputService Key Held Problems

I’m trying to make it so that my webs stay visible while im holding either Q or E and I’m in need of help with the holding down of keys through a remote event because I can’t quite get it, because once the InputEnded function runs it fires the remote again and it messes it up, any help would be appreciated alot thanks!

Local Script :

local QHeld = false
local EHeld = false

UIS.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.E then
		if not EHeld then
			EHeld = true
			local E = 2
			Web:FireServer(mouse.Hit, E , mouse.Target, EHeld)
		end
	end
	if input.KeyCode == Enum.KeyCode.Q then
		if not QHeld then
			QHeld = true
			local Q = 1
			Web:FireServer(mouse.Hit, Q , mouse.Target, QHeld)
		end
	end
end)

UIS.InputEnded:Connect(function(input, isTyping)
	if isTyping then return end
	if input.KeyCode == Enum.KeyCode.E then
		if EHeld then
			EHeld = false
			local E = 2
			Web:FireServer(mouse.Hit, E, mouse.Target, EHeld)
		end
	end
	if input.KeyCode == Enum.KeyCode.Q then
		if QHeld then
			QHeld = false
			local Q = 1
			Web:FireServer(mouse.Hit, Q, mouse.Target, QHeld)
		end
	end
end)

What exactly is the problem? Can you post the code to the server-side script or is it strictly a client-side problem?

The problem is just that I need to pass the QHeld and EHeld bool values but when I do it I have to fire the remote and it messes up the whole thing

It’s above the script I just didnt add it since it wasnt really apart of the problem.

How exactly does it mess it up? Please be very specific, I’m very confused as to what your problem is.

Basically I check on the server the value of EHeld and QHeld and I print it, but the thing is when the InputEnded function passes through it messes up that while loop for when held is true for both bools and it goes infinitely until I press one of the keys again, hold on let me show you what I mean

Ok for future reference please never use the terminology of “it messes up” to describe an issue.

Anyways this seems like a problem on the server. Would you mind posting the server-sided code?

That wasn’t even the whole issue and also it was kind of hard to explain

Server Script :

Web.OnServerEvent:Connect(function(plr, hit, letter, target, held)
	local LeftHand = plr.Character:WaitForChild("Left Arm")
	local RightHand = plr.Character:WaitForChild("Right Arm")
	local webShooterR = RightHand:WaitForChild("webShooterR")
	local webShooterL = LeftHand:WaitForChild("webShooterL")
	
	local rAttach = RightHand:WaitForChild("AttachRight")
	local lAttach = LeftHand:WaitForChild("AttachLeft")
	
	local char = plr.Character
	local humrp = char:WaitForChild("HumanoidRootPart")
	local hum = char:WaitForChild("Humanoid")
	
	local Animations = game.ServerStorage.Animations
	
	while held do
		game:GetService("RunService").Stepped:Wait()
		print(held)
	end

This should work:

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(key)

if key.KeyCode == Enum.KeyCode.A then --Replace A with what you want

--what you want to happen

end

end)

That’s not my problem but thanks for the potential help

1 Like

I see your problem here.

This will open a loop that will never end. Every time an event is fired, it connects it to a function. If there are multiple fires, then the event will connect each fire to a different function instance.

Basically - store held above the event function.

I can’t do that because I’m passing the bool values through the Remote I’m firing from the client

Unfortunately that’s what is causing your problem so you will definitely have to do that.

Here’s a quick mock up of a solution.

local PlayersHolding = {}
Web.OnServerEvent:Connect(function(plr, hit, letter, target, held)
	local LeftHand = plr.Character:WaitForChild("Left Arm")
	local RightHand = plr.Character:WaitForChild("Right Arm")
	local webShooterR = RightHand:WaitForChild("webShooterR")
	local webShooterL = LeftHand:WaitForChild("webShooterL")
	
	local rAttach = RightHand:WaitForChild("AttachRight")
	local lAttach = LeftHand:WaitForChild("AttachLeft")
	
	local char = plr.Character
	local humrp = char:WaitForChild("HumanoidRootPart")
	local hum = char:WaitForChild("Humanoid")
	
	local Animations = game.ServerStorage.Animations
	
	PlayersHolding[plr] = held --Ok not really ideal to use plr as the disctionary but you know how to fix that or whatever 
--ALSO this is a HUGE  security risk so fix that lol
end

while true do
		for plr, holding in pairs(PlayersHolding) do
              if holding then
                 --Do yo thang
              end
       end
end

Essentially - you can fire the same RemoteEvent but it will act as a totally new function and will keep it open forever in your original code.
Here we use your original Remote to set the value of a table which a further loop will constantly check.
The further loop will constantly loop through the table of players holding and it will do its thing for each player

Could I possibly just make a whole new remote that just handles the bool values?

You can do whatever you want BUT you CANNOT have a loop inside one of them unless you have a variable on the outside of it that can stop the loop.

Hmmm, very interesting indeed Mr.Ssspencer, well I’ll see what I can do

I hope my explanations made sense and my pseudocode was a good resource. If you have any questions just continue to reply to me and I’ll answer them in this thread.

If not and this helped you solve the issue be sure to mark it as solved.

Yea spencer its currently not solved but Im gonna try making a remote event that handles just the bools and well see how that goes, see you in probably a few minutes :call_me_hand: :sweat_smile:

But on a serious note thank you for actually helping!