Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to change from onKeyDown to UserInputServices?

I was making a sort of script that moves a boat a WHILE back and now I wanted to finish it. It didn't work so well. Now, I want to convert the tool to a Tool and not a HopperBin but I also need to find a way to do what the onKeyDown event did with UserInputService. There doesn't seem to be any tutorials on the wiki to do this. I want the user to equip the tool, find the ship, and then move it when 'w', 'a', 's' and 'd' is pressed. I tried adapting the methods for controls found in this tutorial: http://wiki.roblox.com/index.php?title=Top_Down_Action/Controls But, what would happen is:

-- I press 'w' to move forward

-- ship moves forward

-- jump off ship

-- can't move character with 'w' anymore

So there must be something wrong.

tl;dr How to replicate onKeyDown and onKeyUp on tools with Roblox's new input system (ContextActionService or UserInputService, I'm not sure)

1 answer

✓ Accepted answer

It's practically the same thing, just with different words.

Example; Press a button to print that key Old, deprecated way (onKeyDown);

local player=game.Players.LocalPlayer
local mouse=player:GetMouse()
function onKeyDown(key)
	print(player.Name..' pressed: '..key)
end
mouse.KeyDown:connect(onKeyDown)

Newer way (UserInputService);

local player=game.Players.LocalPlayer
local uis=game:GetService("UserInputService") --Getting the UserInputService
uis.InputBegan:connect(function(key) --InputBegan=KeyDown
	print(player.Name..' pressed: '..tostring(key.KeyCode))
end)

If you wanted a player to press a specific button (with UIS);

local player=game.Players.LocalPlayer
local uis=game:GetService("UserInputService") --Getting the UserInputService
uis.InputBegan:connect(function(key)
	if key.KeyCode==Enum.KeyCode.A then --If they press A on the keyboard
	print(player.Name..'pressed A!')
end)

2 comments

0

THANK YOU SO MUCH I LOVE YOU

0

No problem. If you have any further questions, just ask.

This discussion is preserved as it was. New answers, comments, and votes are closed.