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

No more KeyDown?

I was wondering why my keyboard-fired scripts were not working when I noticed that ROBLOX has discontinued the KeyDown/Up events on both Mouse and Gui objects. There is an event "InputBegan" that says I can get an Instance of keydown, but I am unfamiliar with it. I don't know any other ways to doing KeyDown/Up events and the ROBLOX Wiki has no examples of this event being used for keyboard scripts. Can someone show me an alternative method?

I was making a script that would make a pair of wheels spin.

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
Car = game.Workspace.Test
LR = Car.LR.Rim_Default.BodyAngularVelocity
RR = Car.RR.Rim_Default.BodyAngularVelocity
throttle = false
brake = false
clutch = false

function presskey(key)
	if key == "w" then
		LR.P = 1250
		RR.P = 1250
		LR.AngularVelocity = (LR.Parent.CFrame * CFrame.new(15,0,0)).p
		RR.AngularVelocity = (RR.Parent.CFrame * CFrame.new(-15,0,0)).p
		throttle = true
		brake = false
	end
	if key == "s" and throttle == false then
		LR.P = 1250
		RR.P = 1250
		LR.AngularVelocity = (LR.Parent.CFrame * CFrame.new(0,0,0)).p
		RR.AngularVelocity = (RR.Parent.CFrame * CFrame.new(0,0,0)).p
		brake = true
	end
end
Mouse.KeyDown:connect(presskey)

2 answers

✓ Accepted answer

Ok so, ever since KeyDown and KeyUp became deprecated, I've used User Input Service. It may be a bit more complicated compared to the old way of doing it but offers more features than just KeyDown and KeyUp. Check all the functions and stuff out here. Ok, so to use it's version of KeyDown, use InputBegan as can be seen in my code:

game:GetService("UserInputService").InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		-- Do something when W is pressed!
	end
end)

This could be put into your code as shown below:

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
Car = game.Workspace.Test
LR = Car.LR.Rim_Default.BodyAngularVelocity
RR = Car.RR.Rim_Default.BodyAngularVelocity
throttle = false
brake = false
clutch = false

game:GetService("UserInputService").InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		LR.P = 1250
		RR.P = 1250
		LR.AngularVelocity = (LR.Parent.CFrame * CFrame.new(15,0,0)).p
		RR.AngularVelocity = (RR.Parent.CFrame * CFrame.new(-15,0,0)).p
		throttle = true
		brake = false
	end
	if input.KeyCode == Enum.KeyCode.S and throttle == false then
		LR.P = 1250
		RR.P = 1250
		LR.AngularVelocity = (LR.Parent.CFrame * CFrame.new(0,0,0)).p
		RR.AngularVelocity = (RR.Parent.CFrame * CFrame.new(0,0,0)).p
		brake = true
	end
end)

1 comment

0

Thanks! It seems easy now that I you applied it to my code. I am assuming the InputEnded event works like KeyUp as well.

This is easier than UndeniableLimited's script.

wait()
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key == "f" then
		print("F was pressed")
	end
end)

It's simple, really.

3 comments

0

Did you even read the question? Apparently mouse.KeyDown is deprecated.

0

Yes, I read it, but it isn't. I showed them an easy way to do it.

1

Deprecated does not mean it is no longer available, it means something that isn't advised and will probably be completely removed at a later date so they want you to change over to an alternative, newer way to avoid any games of yours breaking in the future.

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