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

Force Field script?

Heres what I tried but it failed for me...

On = true

script.Parent.MouseButton1Down:connect(function ()
if On == false then
On = true
Tools = script.Parent.Parent.Parent.Parent.Character:GetChildren()
for i = 1,#Tools do
if Tools[i].className == "Tool" then
Tools[i].Parent = script.Parent.Parent.Parent.Parent.Backpack
end
end
Connection = script.Parent.Parent.Parent.Parent.Character.ChildAdded:connect(function (c)
if c.className == "Tool" then
c.Parent = script.Parent.Parent.Parent.Parent.Backpack
end
end)
F = Instance.new("ForceField",script.Parent.Parent.Parent.Parent.Character)
else
On = false
F:Remove()
Connection:connect()
end
end)

2 answers

I find that using a bool value works better than using an in-script true/false value. Replace On with something like this:

(First insert a bool value into the script called On)

local On = script:WaitForChild("On")

There you go, that was your only problem! I used to run into the same issue. Hope this helps.

2 comments

0

You should not use a BoolValue unless necessary...

0

@NotsoPenguin, why not? It doesn't cause problems, I don't believe. Plus for this you could have it be called "ForceField" and the value is true/false (On/off) which does make sense

Now I see what happened... This is the fixed version...

On = false

script.Parent.MouseButton1Down:connect(function ()
if On == false then
On = true
Tools = script.Parent.Parent.Parent.Parent.Character:GetChildren()
for i = 1,#Tools do
if Tools[i].className == "Tool" then
Tools[i].Parent = script.Parent.Parent.Parent.Parent.Backpack
end
end
Connection = script.Parent.Parent.Parent.Parent.Character.ChildAdded:connect(function (c)
if c.className == "Tool" then
c.Parent = script.Parent.Parent.Parent.Parent.Backpack
end
end)
F = Instance.new("ForceField",script.Parent.Parent.Parent.Parent.Character)
else
On = false
F:Remove()
Connection:disconnect()
end
end)

1 comment

0

Please don't use the answer.

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