I am making a script that when you press "f" the forcefield gets attached to you. When you press it again it will go away. Here is a script I am working on,
local script = script.Parent game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key) if string.lower(key) == "f" then -- don't know what to put here end end)
You create a ForceField using the new
method of Instance
, which may look familiar to you:
Instance.new("ForceField", game.Players.LocalPlayer.Character) --first argument is the 'classname', second (which is optional) is where to parent the created Object.
By default, this is named 'ForceField', so the inside of that if
statement could look like this:
if game.Players.LocalPlayer.Character:FindFirstChild("ForceField") then game.Players.LocalPlayer.Character.ForceField:Destroy() else Instance.new("ForceField", game.Players.LocalPlayer.Character) end
Use booleans so the script knows the state of the forcefield (on or off).
local script = script.Parent local Shield = false FF = Instance.new("ForceField")
Then code this something along the lines like this, using if
and else
.
if string.lower(key) == "f" then local FF2 = FF:clone() if Shied == false then Shield = true local FF2 = FF:clone() FF2.Parent = game.Players.LocalPlayer.Character elseif Shield == true then if game.Players.LocalPlayer.Character:findFirstChild("ForceField") == nil then return end Shield = false FF2:Destroy() end
^^This should be in place of the if
statement that's in your anonymous function.
I don't have any idea on how to make it disappear when hit again, but to give them one when they hit it once, do this.
local script = script.Parent game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key) if string.lower(key) == "f" then local ff = Instance.new("ForceField") --Creates the ForceField ff.Parent = game.Players.LocalPlayer.Character --Assigns the parent to the ForceField end end)