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

Trying to make a script that is fired when a certain key is pressed. Can anyone help?

So uh, I've been trying to make this script work for some time now with the UserInputService yet I still can't manage to find the solution. I am fairly new to this, so help would be appreciated.


local Player = game.Players.LocalPlayer


local anim1 = Instance.new("Animation")
anim1.AnimationId = "http://roblox.com/Assets?ID=2294643345"
		


UIS = game:GetService("UserInputService")



UIS.InputBegan:connect(function(key)
	if key.KeyCode == Enum.KeyCode.Q then
		print("Pressed!")
	
		
local Fire1 = Instance.new("Fire", Player.Character.LeftHand)
      Fire1.Color = Color3.new(236, 139, 70)
      Fire1.SecondaryColor = Color3.new(255, 0, 0)
      Fire1.Heat = -20
      Fire1.Size = 4

local Fire2 = Instance.new("Fire", Player.Character.RightHand)
      Fire2.Color = Color3.new(236, 139, 70)
      Fire2.SecondaryColor = Color3.new(255, 0, 0)
      Fire2.Heat = -20
      Fire2.Size = 4
		
local animation1 = Player.Character.Humanoid:LoadAnimation(anim1)
     animation1:Play()

wait(3)
Fire1:destroy()
Fire2:destroy()

end


end)

I am pretty sure the script doesn't go past the

function onKeyPress(inputObject, gameprocessed)
	if inputObject.KeyCode == Enum.KeyCode.Q then

part.

Thank you.

3 comments

0

Is it a localscript? and where is the script located

0

It is a localscript, and it's just located in workspace.

0

then it wont work, put the local script in starterplayer scripts

2 answers

✓ Accepted answer

Not sure if you read my comment already, but as i said, putting a localscript in workspace wont work, you have to put it in starterplayerscripts, i just tried it and it printed out "pressed" so its working

1 comment

0

Ohh that’s why... Thank you very much!

This is what I use in my games.

game:GetService("UserInputService").InputBegan:Connect(function(input,event)
	if input.KeyCode == Enum.KeyCode.Q then
		-- [code]
	end
end)

Make sure you use this in a local script!

1 comment

0

It still doesn't seem to detect that I pressed the key.

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