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)
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)
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.