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

Script not working?

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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)

1 answer

Log in to vote
0
Answered by
Discern 1007 Moderation Voter
10 years ago

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

0
Thanks you I only been Playing Roblox for about 1 Month and started scripting 2 weeks ago so I'm fairly new to this kinda stuff. UserOnly20Characters 890 — 10y
0
Np. Mind accepting my answer? Discern 1007 — 10y
0
BTW, your Username is not 16, but 18 characters long! :P Michael007800 144 — 10y
Ad

Answer this question