How would i make input began work as i want it to?

Hey there! so, i have an input began script where it detects if the player presses “W” “A” or “D” but i have a problem.

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	
	if input.KeyCode == W or input.KeyCode == A or input.KeyCode == D then
		local check = ray()
		if check == false then return end
		
		if UserInputService:IsKeyDown(input.KeyCode) then
			Remote:FireServer(input.KeyCode)
		end
	end
end)

so as you can see, its an input began script, simple thing right? But my problem is that it doesn’t detect it when i hold my key.

lets say i get to a part where i have to press a movement key, i hold “W” to get there and im in range/is ray detected but its not firing. why? i have to let go of “W” and then i can press “W” again and it will work.

I dont want that.

How would i fix this?

Thanks!!

here, you just needed to loop it

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	
	if input.KeyCode == W or input.KeyCode == A or input.KeyCode == D then
		while UserInputService:IsKeyDown(W) or UserInputService:IsKeyDown(A) or UserInputService:IsKeyDown(D) do
			task.wait()
			local check = ray()
			if check == false then return end
		end
		
		if UserInputService:IsKeyDown(input.KeyCode) then
			Remote:FireServer(input.KeyCode)
		end
	end
end)
1 Like

msix29 is right, you do need to loop it - however the script he made isn’t exactly going to solve your problem and would actually crash your game. Here is a script I have made for you:

local uis = game:GetService("UserInputService")

while true do
	game:GetService("RunService").RenderStepped:Wait()
	local keydown
	if uis:IsKeyDown(Enum.KeyCode.W) then
		keydown = Enum.KeyCode.W
	elseif uis:IsKeyDown(Enum.KeyCode.A) then
		keydown = Enum.KeyCode.A
	elseif uis:IsKeyDown(Enum.KeyCode.D) then
		keydown = Enum.KeyCode.D
	end
	if keydown then
		local check = ray()
		if check then
			Remote:FireServer(keydown)
		end
	end
end
1 Like