Jump to content

[Question] Checking if a key is held


kieran

Recommended Posts

Hello, I want to make a script for a ladder, I am half way there, and my player moves up and down.....  But the problem is I have to spam W!  Is there any way to check if a key is being held rather than checking if it is pressed? Here's the part of the script where I move the player.

function movePlayer(key, state) 
	if key == "w" and state then --If the key is w and it's pressed.
		x, y, z = getElementPosition ( localPlayer )
		setElementPosition ( localPlayer, x, y, z+1, false ) --I move the player after I get their position.
		setElementFrozen( localPlayer, true )
	elseif key == "s" and state then --If it's s.
		x, y, z = getElementPosition ( localPlayer )
		setElementPosition ( localPlayer, x, y, z-1, false ) --They move down.
		setElementFrozen( localPlayer, true )
	end
end

function addTheHandlers()
	addEventHandler("onClientKey", root, movePlayer) --When the player hits the key they will be moved up or down, I want it to be if they HOLD the key down they move.
end

addEvent("addLadderHandlers", true)
addEventHandler("addLadderHandlers", root, addTheHandlers)

I know that onClientKey basically checks the key presses, so would I need to add an onClientRender and check the pressed key and state? Or is their an easier way/handler?

Would appreciate ideas/solutions, thanks.

Link to comment

Yeah, well it still doesn't let me just hold and climb, it only registers the key press once, then stops til I press it again..... 

Also getKeyState done the exact same...  I want to be able to hold it and basically use it like onClientRender rather than onClientResourceStart, if that makes sense...

function movePlayer()
	if getKeyState ("w") == true then
		x, y, z = getElementPosition ( localPlayer )
		setElementPosition ( localPlayer, x, y, z+1, false )  
	elseif getKeyState ("s") == true then
		x, y, z = getElementPosition ( localPlayer )
		setElementPosition ( localPlayer, x, y, z-1, false )  
	end
end

function addTheHandlers()
	setElementFrozen( localPlayer, true )
	addEventHandler("onClientKey", root, movePlayer) --Want this to be constantly checked...
end

Okay problem was onClientKey, but how can I sort this? I am a bit mind f**ked at the moment...

Edited by kieran
Updated code
Link to comment
  • Moderators
function movePlayer(button, press)
	if press then
		if button == "w" then
			local x, y, z = getElementPosition ( localPlayer )
			setElementPosition ( localPlayer, x, y, z+1, false )  
		elseif button == "s" then
			local x, y, z = getElementPosition ( localPlayer )
			setElementPosition ( localPlayer, x, y, z-1, false )  
		end
	else
		--release	
	end
end

function addTheHandlers()
  setElementFrozen( localPlayer, true )
  addEventHandler("onClientKey", root, movePlayer) --Want this to be constantly checked...
end

 

You do not need getKeyState if you use onClientKey, only if you use onClientRender.

 

Link to comment
4 hours ago, IIYAMA said:

function movePlayer(button, press)
	if press then
		if button == "w" then
			local x, y, z = getElementPosition ( localPlayer )
			setElementPosition ( localPlayer, x, y, z+1, false )  
		elseif button == "s" then
			local x, y, z = getElementPosition ( localPlayer )
			setElementPosition ( localPlayer, x, y, z-1, false )  
		end
	else
		--release	
	end
end

function addTheHandlers()
  setElementFrozen( localPlayer, true )
  addEventHandler("onClientKey", root, movePlayer) --Want this to be constantly checked...
end

 

You do not need getKeyState if you use onClientKey, only if you use onClientRender.

 

 

That's not going to work. It only registers it once as he presses it.

function movePlayer(kButton, kState)
	if kState then
		moveTimer = setTimer(function()
			if getKeyState ("w") == true then
				x, y, z = getElementPosition ( localPlayer )
				setElementPosition ( localPlayer, x, y, z+1, false )  
			elseif getKeyState ("s") == true then
				x, y, z = getElementPosition ( localPlayer )
				setElementPosition ( localPlayer, x, y, z-1, false )  
			end
		end, 500, 0)
	elseif (kButton == "w" or kButton == "s") and isTimer(moveTimer) then
		killTimer(moveTimer)
	end
end

function addTheHandlers()
	setElementFrozen( localPlayer, true )
	addEventHandler("onClientKey", root, movePlayer) --Want this to be constantly checked...
end

Try this one.

Edited by NeXuS™
  • Thanks 1
Link to comment
8 hours ago, NeXuS™ said:

That's not going to work. It only registers it once as he presses it.


function movePlayer(kButton, kState)	if kState then		moveTimer = setTimer(function()			if getKeyState ("w") == true then				x, y, z = getElementPosition ( localPlayer )				setElementPosition ( localPlayer, x, y, z+1, false )  			elseif getKeyState ("s") == true then				x, y, z = getElementPosition ( localPlayer )				setElementPosition ( localPlayer, x, y, z-1, false )  			end		end, 500, 0)	elseif (kButton == "w" or kButton == "s") and isTimer(moveTimer) then		killTimer(moveTimer)	endendfunction addTheHandlers()	setElementFrozen( localPlayer, true )	addEventHandler("onClientKey", root, movePlayer) --Want this to be constantly checked...end

Try this one.

Thanks, but now if I go down when climbing up my player gets the W key stuck.....  I have no clue why. :$

local moveTimer

function movePlayer(key, state)
	if state then
		moveTimer = setTimer(function()
		if getKeyState ("w") == true then	
			x, y, z = getElementPosition ( localPlayer )
			setElementPosition ( localPlayer, x, y, z+0.5, false )  
		elseif getKeyState ("s") == true then
			x, y, z = getElementPosition ( localPlayer )
			elseif (key == "w" or key == "s") and isTimer(moveTimer) then
				killTimer(moveTimer)
			end
		end, 250, 0)
	end
end

function addTheHandlers()
	setElementFrozen( localPlayer, true )
	addEventHandler("onClientKey", root, movePlayer)
end

addEvent("addLadderHandlers", true)
addEventHandler("addLadderHandlers", root, addTheHandlers)

By the way, the player is in a colShape if that has something to do with it.....  Even when I exit the col shape I still have W stuck on.

Edit

fixed it, checked timer before it started.

Edited by kieran
Link to comment
6 minutes ago, NeXuS™ said:

What do you mean by "stuck on"?

Basically I was checking if timer was active inside the timer, so it could be triggered twice if my player went up pressing w and then down pressing s without leaving the col....  I don't know if I was just checking the timer wrong or what, but the following works....

local moveTimer

function movePlayer(key, state)
	if state then
		if (key == "w" or key == "s") and isTimer(moveTimer) then
			killTimer(moveTimer)
		end
		moveTimer = setTimer(function()
		if getKeyState ("w") == true then	
			x, y, z = getElementPosition ( localPlayer )
			setElementPosition ( localPlayer, x, y, z+0.5, false )  
		elseif getKeyState ("s") == true then
			x, y, z = getElementPosition ( localPlayer )
			elseif (key == "w" or key == "s") and isTimer(moveTimer) then
				killTimer(moveTimer)
			end
		end, 150, 0)
	end
end

function addTheHandlers()
	setElementFrozen( localPlayer, true )
	addEventHandler("onClientKey", root, movePlayer)
end

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...