A weird bug[?] in my script

Hello! :wave:

I want to figure out how to get all children inside a parent, but something weird has happened.

Recently I’ve encountered a strange change or bug, and it broke some of my scripts. For example, before Roblox went down, when I type for a, b in pairs(game.Workspace:GetChildren()) do, I expected it to have no errors and get all of Workspace’s Children, and it did just that. After Roblox went back up and when I load my game in Studio and play tested, I found out that some of my scripts containing something similar to for a, b in pairs() do, it gets an error in the Output saying

image

It seemed like something must have been changed while Roblox was down, or i was unaware of a new update.
Here’s what I’ve typed in my scripts. Here, line 23 is the problem:


I tried to see what would happen if i printed out a

Now the script just prints 1

Here’s a picture of my script that is supposed to create a LineForce inside all of MeshParts in Workspace and Parent it to ‘a’ (every MeshPart in Workspace), it used to work before but now, but ‘a’ is now number 1 so the LineForce is unable to be Parented because ‘a’ turned into a number.

I’ve never encountered this kind of problem before and i’m very confused of this situation.

If you can help me figure the problem out, I’d be very grateful :smiley:

a is the index, rather than the value. You would want to replace a with b.

1 Like

Thanks for the reply!
I’ve replaced them, and it doesn’t seem to work. I’ve got the same error. I’m unsure, but I think the a and b can be named to anything and they both will have the same information because I’ve also tried naming them to something random after changing the script accordingly.

Can I see the output? Also try

workspace[b].Classname

Here’s the output after i changed it:
image

I’ve also tried workspace[b].Classname but that didn’t seem to change anything so i switched back to the old one.
Here’s the full script if that helps. The script is also placed inside ServerScriptService.


local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local remoteFunction = ReplicatedStorage:WaitForChild("Spawn")
 
local function onCreatePart(Player, CamPosition) -- Player's name and their camera position
	local newPart = Instance.new("Part")
	newPart.Position = CamPosition
	newPart.Parent = workspace
		for a, b in pairs(game.Workspace:GetChildren()) do
			if a.ClassName == "Part" or a.ClassName == "MeshPart" then
			local LineForce = Instance.new("LineForce")
			LineForce.Attachment0 = a
			LineForce.Attachment1 = newPart
			LineForce.ApplyAtCenterOfMass = true
			LineForce.InverseSquareLaw = true
			LineForce.ReactionForceEnabled = true
			LineForce.Magnitude = newPart.Size.X * 100
			LineForce.Parent = a
		end
	end
end

remoteFunction.OnServerEvent:Connect(onCreatePart) -- When a client fires to server
for index, value in pairs (children) do
-- index is a Number
-- value is the object (part, folder, model, etc)
end

Like @alwayscritics said, you’re setting the Parents of various objects in your script to a Number (a), not an actual object (b)

Name your variables better and you won’t run into this issue again.

You just set the wrong variables, a is the index and b is the object.
I just replaced a with b.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteFunction = ReplicatedStorage:WaitForChild("Spawn")

local function onCreatePart(Player, CamPosition) -- Player's name and their camera position
	local newPart = Instance.new("Part")
	newPart.Position = CamPosition
	newPart.Parent = workspace
	for a, b in pairs(game.Workspace:GetChildren()) do
		if b.ClassName == "Part" or b.ClassName == "MeshPart" then
			local LineForce = Instance.new("LineForce")
			LineForce.Attachment0 = b
			LineForce.Attachment1 = newPart
			LineForce.ApplyAtCenterOfMass = true
			LineForce.InverseSquareLaw = true
			LineForce.ReactionForceEnabled = true
			LineForce.Magnitude = newPart.Size.X * 100
			LineForce.Parent = b
		end
	end
end

remoteFunction.OnServerEvent:Connect(onCreatePart) -- When a client fires to server
2 Likes

if b.ClassName == "MeshPart"

Alternatively you could use "if b:IsA(“MeshPart”).

Oh that’s what he meant, my bad. The script works perfectly now!
I’m not sure what caused some of the scripts to break even though it worked before.