Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script not working?

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

✓ Accepted answer

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

3 comments

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.

0

Np. Mind accepting my answer?

0

BTW, your Username is not 16, but 18 characters long! :P

This discussion is preserved as it was. New answers, comments, and votes are closed.