So, I wanna check when a player pressed a key to make a sort of "attack."
This is the current code which isn't working:
game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(char) local UserInputService = game:GetService("UserInputService") local key = Enum.KeyCode.R local pressedkey = UserInputService:GetKeysPressed() local function forceField() for _, keypog in pairs(pressedkey) do if (pressedkey.KeyCode == key) then local part = Instance.new("Part",workspace) part.Shape = Enum.PartType.Ball part.Color3 = Color3.new(159, 243, 233) part.Position = char.Torso.Position part.CanCollide = true part.Anchored = true wait(1) part.Size = Vector3.new(4.623, 4.623, 4.623) part.Transperency = 0.1 wait(1) part.Size = Vector3.new(5.305, 5.305, 5.305) part.Transperency = 0.2 wait(1) part.Size = Vector3.new(6.304, 6.304, 6.304) part.Transperency = 0.3 wait(1) part.Size = Vector3.new(7.569, 7.569, 7.569) part.Transperency = 0.4 wait(1) part.Size = Vector3.new(8.88, 8.88, 8.88) part.Transperency = 0.5 wait(1) part.Size = Vector3.new(10.247, 10.247, 10.247) part.Transperency = 0.6 wait(1) part.Size = Vector3.new(11.69, 11.69, 11.69) part.Transperency = 0.7 wait(1) part.Size = Vector3.new(13.387, 13.387, 13.387) part.Transperency = 0.8 wait(1) part.Size = Vector3.new(15.039, 15.039, 15.039) part.Transperency = 0.9 wait(1) part:Destroy() end end end end) end)
As DetectiveRaie said, you should use :InputBegan() to simply detect user input. Also remember UIS only works in 'LocalScript' not 'ServerScript' ( RECOMMENDED ONE )
Basic Code:
local UserInputService = game:GetService("UserInputService") local requiredKey = Enum.KeyCode.E -- key needed to run the function UserInputService.InputBegan:Connect(function(key, gameProcessed) if key.KeyCode == requiredKey then -- Use remote event/function to comunicate with the server wait(10) -- cooldown end end)
Next, you will need to use remote function/event since it's client sided code if you want your attacks or stuff to be seen by all players/clients ( server )
Another way of doing it by using :GetPressedKeys() ( LESS RECOMMENDED )
Your code is actually fine and correct but there is something that makes it not work.
Before getting into the code. There' 2 ways I usually use to detect input with :GetPressedKeys(), which are UserInputService.JumpRequest and Humanoid.Running or simply just use white loop
Basic Code: ( White Loop Usage )
local UserInputService = game:GetService("UserInputService") local requiredKey = Enum.KeyCode.R local function getPressedKey() local pressedKey = UserInputService:GetKeysPressed() for _, key in pairs(pressedKey) do if key.KeyCode == requiredKey then print("Pressed: " , key.KeyCode) -- Don't forget to use remote event/function wait(10) -- cooldown end end end while wait(1) do getPressedKey() end
REMINDER: ALL stuff you want to do needs to be done on the server by using remotes
FYI: You can use remote function instead of remote event to make a built in cooldown for this
Hello, from what I can see you have the code right, but instead of trying to get when a key is pressed I would check when any input from the UserInputService is detected using :InputBegan()
.
I believe this is what you are looking for, take a look at lines 5-8, and let me know if I am wrong. Have a great day, Raie.