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

Force Field script?

Asked by
narfh 0
9 years ago

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

Log in to vote
0
Answered by 9 years ago

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.

0
You should not use a BoolValue unless necessary... NotsoPenguin 705 — 9y
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 alphawolvess 1784 — 9y
Ad
Log in to vote
0
Answered by
narfh 0
9 years ago

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)
0
Please don't use the answer. woodengop 1134 — 9y

Answer this question