So I was practicing a little bit of scripting in studio, whilst watching Peaspod’s Advanced Tutorial on key pressing events. I scrolled through the comments a little and people have said that the KeyDown and KeyUp function is outdated and that I should be using the UserInputService event instead. So I did a little bit of research on the UserInputService | Documentation - Roblox Creator Hub and I also tried various ways in my script (It was written just to mess around with events and stuff from the object browser), however I still can’t get this to work and I still don’t know how to do this.
--The script I was messing around with just to the the KeyDown and KeyUp events
local tool = script.Parent
local player = game.Players.LocalPlayer
local playerInputService = game.Players.LocalPlayer:GetService()
tool.Equipped:Connect(function()
playerInputService.UserInputService:connect(function(key)
if key == "h" then
print("You a noob")
else
print("You still a noob")
end
end)
playerInputService.UserInputService:connect(function(key)
if key == "h" then
print("You are the best for letting go of me!")
else
print("Get off of me >:(")
end
end)
end)
Here is my code. It is giving me an error: [15:51:05.223 - GetService is not a valid member of Player “Players.HexTheSpy”. Hopefully You guys can help me.
Thank you very much!!!
First of all you can only get server from game:GetService() and second of all you haven’t defined which service to get so that line should be like this
local playerInputService = game:GetService("UserInputService")
So this should be the entire script with no function errors:
local tool = script.Parent
local player = game.Players.LocalPlayer
local playerInputService = game:GetService("UserInputService")
tool.Equipped:Connect(function()
playerInputService.InputBegan:connect(function(input)
if key == "h" then
print("You a noob")
else
print("You still a noob")
end
end)
playerInputService.InputBegan:connect(function(key)
if key == "h" then
print("You are the best for letting go of me!")
else
print("Get off of me >:(")
end
end)
end)
local tool = script.Parent
local player = game.Players.LocalPlayer
local playerInputService = game:GetService("UserInputService")
tool.Equipped:Connect(function()
playerInputService.InputBegan:connect(function(input)
if input.KeyCode == "h" then
print("You a noob")
else
print("You still a noob")
end
end)
--Removed the second function since it isn't really necessary at all
end)
Oh wait i almost forgot that the if statement won’t work since it doesn’t work like that so you will need to add input.KeyCode.H In it So it should be like this for the inputbegan function
playerInputService.InputBegan:connect(function(input)
if input.KeyCode == input.KeyCode.H then
print("You a noob")
else
print("You still a noob")
end
end)
There is one problem though, if you unequip the tool, it will still print if you press “h”
I also suggest you use Enums instead of strings:
local tool = script.Parent
local player = game.Players.LocalPlayer
local playerInputService = game:GetService("UserInputService")
local ToolEquipped = false
tool.Equipped:Connect(function()
ToolEquipped = true
end)
tool.Unequipped:Connect(function()
ToolEquipped = false
end)
playerInputService.InputBegan:Connect(function(input)
if ToolEquipped == true then
if input.KeyCode == Enum.KeyCode.H then
print("You a noob")
else
print("You still a noob")
end
end
end)
Yeah the reason why i had to explain each mistake one by one instead of telling the entire correct script is because i don’t want to get a feedback for sharing scripts