Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make forefield appear on you when you press "f"?

Asked by 9 years ago

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)

3 answers

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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
Ad
Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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.

Log in to vote
-1
Answered by
Relatch 550 Moderation Voter
9 years ago

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)
0
Please don't give an answer unless you can fully answer the question. I don't mean to be rude but it's not exactly helpful. :/ adark 5487 — 9y
0
...Atleast I tried Relatch 550 — 9y

Answer this question