How do I make when a player presses a keycode they receive a forcefield

How do I make when a player presses a keycode they receive a forcefield

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.Keycode == Enum.KeyCode.X then
		-- The forcefield giver code
	end
end)
2 Likes
local UserInputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer

UserInputService.InputBegan:Connect(function(input)
	if input.Keycode == Enum.KeyCode.X then
		-- The forcefield giver code
       local new = instance.new("Forcefield")
      new.Parent = plr
	end
end)
5 Likes

You’ll have to make use of RemoteEvents and a Server Script.

Once the player presses the Key, an event should fire and tell the server to provide him with a forcefield. If you do that locally, the forcefield won’t be visible to other players and the person themselves can still be damaged.

If you’re unsure on how to create this, I suggest the following materials:

Client-To-Server
Remotes and Events

Good luck!

3 Likes

httpDerpyy did a better explanation, please read his reply.

1 Like

Ok thank you :grinning: :grinning:

1 Like

It says this in output
lua Keycode is not a valid member of InputObject "InputObject"

I really suggest you read the materials I’ve provided you with and attempt to create something by yourself. If you just copy and paste code you won’t be able to learn for yourself! Study the links I’ve provided you and I’m 100% sure you’ll be able to assemble this on your own.

Change Keycode to KeyCode (30 limit)

Ok thank you :grinning: :smiley:

1 Like

I did read what you sent already

Remember that caps matter in code!

i agree with this one too, you might learn a lot from it

one more thing, you need to change it to Instance.new, capitalization matters

and then the forcefield should go inside of the plr.Character, otherwise you dont see it

1 Like

Ok thank you really appreciated