I can't seem to find why this Forcefield script isn't working can anyone help me?
ForceField = false plr = game.Players.LocalPlayer local torso = plr.Character:FindFirstChild("Torso") plr:GetMouse().KeyDown:connect(function(key) if key == "f" and forcefield == false then if torso then forcefield = true local FF = Instance.new("ForceField", torso) end elseif key == "f" and forcefield == true then ForceField = false torso:FindFirstChild("ForceField"):Destroy() end end)
There are 2 things that I can point out in your script:
Variables need to remain case sensitive throughout the whole script. You have "ForceField" on lines 1 and 11, but you have "forcefield" on lines 7, 5, and 10. Keep them all in the same capitalization.
This LocalScript (Just check to make sure it's in one), is running before the character loads. Therefore, Line 3 will try to find a Torso before the character exists because it runs so fast.
Check this out:
ForceField = false plr = game.Players.LocalPlayer repeat wait() until plr.Character repeat wait() until plr.Character.Torso local torso = plr.Character:FindFirstChild("Torso") plr:GetMouse().KeyDown:connect(function(key) if key == "f" and ForceField == false then if torso then ForceField = true local FF = Instance.new("ForceField", torso) end elseif key == "f" and ForceField == true then ForceField = false torso:FindFirstChild("ForceField"):Destroy() end end)
Things to check for:
Make sure this script is in StarterGui or StarterPack
Make sure this script is a LocalScript