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

[SOLVED] Power disabling script only works in studio and not in game?

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

0

Don't use while wait() do if you want an infinite loop use while true do

0

Also why did you disable accurate play solo.

0

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?

0

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.

0

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.

0

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?

0

I just looked it up, and this is what it shows: https://gyazo.com/c68157dfde4ccf11b6d64303dd35fc4d

0

It doesn't appear to be disabled. Any ideas?

0

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?

0

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.

0

I'm not entirely sure though, but while wait() do has always worked for me, so that's normally what I use.

0

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.

0

That is true. I'll keep that in mind.

1 answer

In the end, I kept on working on it, and eventually figured it out. I'm still not sure why it worked in Studio but not the actual game, but I just ended up remaking it entirely. I feel like there's a simpler way to do something like this, but here's what I ended up with:

-- Other Variables
local toDisable = false
local ffExists = false
local disabled = false
local canEnable = true
local power

-- Event
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		
		while wait() do
			
			repeat -- Repeats until the power is loaded in.
			for i,object in pairs(char:GetChildren()) do
				if object.ClassName == "ForceField" and canEnable then -- "canEnable" helps prevent it from disabling the script multiple times, over and over.
					
					toDisable = true -- If a force field is found in the player, it sets these variables.
					ffExists = true
					canEnable = false
					
				elseif object.ClassName == "ForceField" and not canEnable then
					ffExists = true -- For when canEnable isn't true, this sets the ffExists variable to true for later on.
				else
					ffExists = false -- If no force fields are found, it sets it to false.
				end
				
				if object.ClassName == "LocalScript" and object:FindFirstChild("OriginalName") then
					
					power = object -- Sets the power variable to the script that we want to disable.
					
				end
			end
			wait()
			
			until power ~= nil
			
			if toDisable and ffExists and not disabled and not power.Disabled then -- Disables the script.
						
				print("Disabled")
				
				toDisable = false
				power.Disabled = true
				disabled = true
				
			elseif disabled and not toDisable and not ffExists and power.Disabled then -- Enables the script.
				
				print("Enabled")
				
				power.Disabled = false
				ffExists = false
				disabled = false
				canEnable = true
				
			end
			
			power.Changed:Connect(function() -- This was mainly for some custom abilities I had, where I could switch between two different powers, but this enabled and disabled the original power script, so I made this to counter that.
				if power.Disabled == false and disabled == true and ffExists then
					
					print("Disabled")
				
					toDisable = false
					power.Disabled = true
					disabled = true
					
				end
			end)
			
		end
		
	end)
end)

I mainly decided to post this just in case it helped anyone, although I doubt many people are going for the same thing that I am.

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