Problem with keybinds

Trying to make code for when you hold z, something happens. However when I press z I don’t get pass print 1. I don’t see any issues in the code, what am I doing wrong?
image


local uis = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
uis.InputBegan:Connect(function(input, gameprocessedevent)
	if gameprocessedevent then return end
print("1") 
	if input.KeyCode == script.Parent.Keybinds.ProvinceSelection.Value then
		print("2")
		local color 

	end
end)

2 Likes

Did you mean to type

if not gameprocessedevent then return end

I forgot the basics of InputBegan but I’m assuming that every time the event is fired gameprocessedevent has a value, so your guard condition would always stop the function everytime it fires

im ngl im a goofball so idk but ye

1 Like

no it doesnt work when I do this

1 Like

i jus found out gameprocessedevent is a boolean :sob: (in my case its usually false)

what about just removing the if statement?

1 Like

because the input keycode isn’t equal to the string values I’m assuming your using. Try this code instead:

local uis = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
uis.InputBegan:Connect(function(input, gameprocessedevent)
	if gameprocessedevent then return end
print("1") 
	if input.KeyCode == Enum.KeyCode[script.Parent.Keybinds.ProvinceSelection.Value] then
		print("2")
		local color 

	end
end)
4 Likes

ack, i interpreted ‘[past] print 1’ as they couldn’t even get to print 1 :skull:

1 Like

well if you want code when you hold z, not press it
you would use heartbeat to check if key is down

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	if UserInputService:IsKeyDown(Enum.KeyCode.Z) then
		print(1)
	end
end)
1 Like

That’s just loops when you don’t need loops, plus you’ll have to check when they let go or it will call it multiple times.

i misunderstood, they only want key pressed, not to constantly check if the keybind is held

1 Like

1st: Any error in the console?
2nd: Running the game locally?
3rd: Does the script run at all, say if you were to put a print statement in the very beginning does it print?

I suggest commenting out the gameprocessable if statement till you get it to work

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.