How to update variables when user holds a button?

Hello everyone,

I want to update variables when a user holds a key - such as the key “w”.

In the code below, I want the lv or look vector to update when the user holds a key. The lv only updates every time the button is pressed down not held.

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.W then
		lv = b_gyro.CFrame.LookVector
		b_velocity.Velocity = Vector3.new(lv.X * speed, lv.Y * speed, lv.Z * speed)
	end
end)

while true do	
	b_gyro.CFrame = CFrame.new(hrp.Position,mouse.Hit.p)
	lv = b_gyro.CFrame.LookVector	
	RunService.Heartbeat:Wait()
end

Thanks for the Help!

1 Like

Your going to need some control variables and your going to need an input ended function

why do I need an input ended, I want to update the look vector variable while the user holds down the “w” button

exactly, so you need to know when they stop holding it…

-- Add a Value that goes +1 
local val =0
inputbegan
holding = true
val=0
while Holding do
val=val+1
if val==5 then
--function here
val=0
end
end
Userinput.INputEnded  ..
holding = false
-- // This was wrote by hand so pls ignore that its bad typed

I think you are misunderstanding, I want to update the lookvector variable when the user is holding the “w” key.

but ur able to do it with that script I sent above? with a while holding true do thing. I just sent an example ^^.

yeah, thanks, can i have an explanation if possible

So basically while holding is a value that is either true or false
if its false it won’t hold anymore // I am bad at explaining sorry.

ok, another problem, when I hold it too long, it just stops.

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == forward_key then 
		holding = true 
		val = 0
	end
	
	while holding do
		val += 1
		if val == 5 then
			b_gyro.CFrame = CFrame.new(hrp.Position,mouse.Hit.p)
			lv = b_gyro.CFrame.LookVector
			b_velocity.Velocity = Vector3.new(lv.X * speed, lv.Y * speed, lv.Z * speed)
			val = 0
			wait()
		end
	end
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	holding = false
	b_velocity.Velocity = Vector3.new(0,0,0) --i think this is the problem, but how do i reset the velocity
end)

You can remove that by removing the val thing. // The val thing was just an example.

local holding=false
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == forward_key then 
		holding = true 
	end
	
	while holding do
			b_gyro.CFrame = CFrame.new(hrp.Position,mouse.Hit.p)
			lv = b_gyro.CFrame.LookVector
			b_velocity.Velocity = Vector3.new(lv.X * speed, lv.Y * speed, lv.Z * speed)
			wait()
	end
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	holding = false
end)

oh, so the val is for like how many seconds the user holds the button

Yeah, it was meant for something like that^^.

No, thats doesn’t seem to work, just retains to the original problem I had

local holding=false
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == forward_key then 
		holding = true 
	end
	
	while holding do
			b_gyro.CFrame = CFrame.new(hrp.Position,mouse.Hit.p)
			lv = b_gyro.CFrame.LookVector
			b_velocity.Velocity = Vector3.new(lv.X * speed, lv.Y * speed, lv.Z * speed)
			wait(1)
	end
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	holding = false b_velocity.Velocity = Vector3.new(0,0,0)
end)

thanks again, @EletricalSpy for helping me, you too, @IProgramForFun