What I'm trying to do is make a script that disables peoples abilities whenever a force field object is detected inside of the character. In Roblox Studio, it works just as intended and disables and reenables the local script each time it should, but in game, it fails to reenable the script. Players will just be spawning in, and a force field is around them right? Then because of that force field, their power script is disabled, but then it's not reenabled after the force field is gone. I don't think this is a problem with FE, but that's what it seems to be.
Server Script in Server Script Service:
-- Other Variables
local disabled = false
-- Event
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
while wait(0.5) do
for i,object in pairs(char:GetChildren()) do
if object.ClassName == "ForceField" then
for i,power in pairs(char:GetChildren()) do
if power:FindFirstChild("OriginalName") and not disabled then
power.Disabled = true
disabled = true
end
end
elseif object:FindFirstChild("OriginalName") and disabled then
object.Disabled = false
disabled = false
end
end
end
end)
end)
Note: These power scripts are local scripts that fire different remote events based on keys pressed to do things in server scripts, in case any of that information is important. Also, as stated earlier, it works just fine in Roblox Studio, which seems very weird, knowing that it doesn't work in the actual game.
Any help is appreciated!

13 comments
Don't use while wait() do if you want an infinite loop use while true do
Also why did you disable accurate play solo.
I used while wait() because if I didn't it would crash the studio. I know I could've put a wait in the while loop, but that way is just more efficient in my opinion. Also, what do you mean by accurate play solo?
while wait() do is not more efficient. And that's not crashing studio. That's a hang. And a few months back "Accurate Play Solo" was introduced and is on by default. It basically lets you toggle in between the server and client mode. Back then the Play Solo the server and client were not separated.
Because there wasn't separation play solo would give inaccurate results. With accurate play solo results should be accurate. You seem to have disabled accurate play solo.
Ah okay, but I still don't see what's wrong with while wait() do, is it not just a while loop with a wait in it?
I just looked it up, and this is what it shows: https://gyazo.com/c68157dfde4ccf11b6d64303dd35fc4d
It doesn't appear to be disabled. Any ideas?
Weird then. Also if you write code in a certain way you should know how it works right? So can you explain why while wait() do works?
Well, if we're speaking English, it makes no sense, but here's how I look at it. The idea of "wait()" in a script can never be nil, right? So I assume when you say while wait() do, it notices that wait() is not equal to nil, so it keeps going forever, and also adds a wait every time it does the loop.
I'm not entirely sure though, but while wait() do has always worked for me, so that's normally what I use.
Exactly. You're using code without knowing how it even works. while true do is clearer so you should use it. Source code always has been, always should be, and always will be, for humans. Write code NOT FOR THE MACHINES EXECUTING IT, but for the HUMANS WHO WILL READ AND EDIT THAT CODE. Code is READ much more than it is WRITTEN.
That is true. I'll keep that in mind.