How to use isKeyDown() on UserInputService?

hello can anyone or anybody could explain or help me how to use isKeyDown on UserInputService
ive been searchin on google and looking on the yt and i dont see or understand it.

TYSM in Advance

2 Likes

Simple:

UserInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.E then
print("E key was pressed")
end
end)

Hope this helped.

11 Likes

Also the IsKeyDown() can be used like what this page says:

2 Likes

i dont see isKeyDown()
what i mean is how to use IsKeyDown()
like when the key has been hold it will fill the water and if its been released the water will stop filling
but ive learn something from you =)

1 Like

Oh you can use InputBegan and InputEnded.

local KeyDown = false

InputService.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.E then
KeyDown = false
end
end)

InputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.E then
KeyDown = true

repeat
wait()
-- function here like adding 10+ to water value
until KeyDown == false
end
end)

Hope this helped aswell…

3 Likes

let me test it =)
i hope it helps

1 Like

it didnt work =(

local KeyDown = false
local InputService = game:GetService("UserInputService")
local water = game.Workspace.Water.Size

InputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		KeyDown = false
	end
end)

InputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		KeyDown = true
	
		repeat
		wait(1)
		water.Y = water.Y + 5
		until KeyDown == false
	end
end)
2 Likes

You can’t do
local water = game.Workspace.Water.Size
It would just be a vector3 value of the water’s size and wouldn’t affect its size when it is changed

1 Like

how do i do it??
help me im new at scripting

1 Like

Try this

local KeyDown = false
local InputService = game:GetService("UserInputService")
local water = game.Workspace.Water.Size

InputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		KeyDown = false
	end
end)

InputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		KeyDown = true
	
		repeat
		wait(1)
		water.Y = water.Y + 5
        game.Workspace.Water.Size = water
		until KeyDown == false
	end
end)
1 Like

Alternatively you can redefine water.
local water = game.Workspace.Water

and then when you are wanting to change the size,

water.Size = water.Size + Vector3.new(0, 5, 0)
3 Likes

IsKeyDown returns a boolean based on if the Enum code you pass is currently being held down. You can check already on the IsKeyDown documentation page for information about what it is and how to use it. If you actually searched, surely you should’ve come across this page, no?

You’ve got an XY Problem going here though. You always want to ask about your actual use case, not about a function and try brute forcing it into your solution. You only later stated your use case which is to have something increase as a key is held down. You do not need IsKeyDown here.

5 Likes

owwwwwww thx for the information =)

1 Like