How to fix for loop not working

Ok, so I made a for loop that checks if the name of an object is equal to another and I look at the folder myself and the name changes to something else, but in the for loop it doesnt detect the change.

here my script

local children = game.Workspace.ForestOrbPlaces:GetChildren()

local randomChild = children[math.random(1, #children)]

game.ReplicatedStorage.Orb.OnServerEvent:Connect(function(plr, currentBreakName, normName)
	for i, v in pairs(script.Parent:GetChildren()) do
		if v.Name == currentBreakName then -- right here it says theres none equal to currentBreakName but when I manually look in the folder there is
			local clonedItem = v:Clone()
			clonedItem.Name = normName
			while wait() do
				wait(.25)
				v.BillboardGui.HeathValue.Value -= plr.Values.Power.Value
				if v.BillbordGui.HeathValue.Value <= plr.Values.Power.Value then
					wait(.25)
					v.BillboardGui.HeathValue.Value = 0
					wait(.25)
					v:Destroy()
					break
				end
			end
			wait(2)
			v.ClickBox.ClickDetector.MaxActivationDistance = 85
			clonedItem.Parent = game.Workspace.OrbsFolder
			if randomChild.AreaTaken.Value == false then
				clonedItem.Position = randomChild.Position
			else
				repeat
					clonedItem.Position = randomChild.Position
				until randomChild.AreaTaken.Value == false
			end
		end
	end
end)

We know that currentBreakName is data coming from the client. So the issue is inside the LocalScript which is invoking that event. Could you please show us the script that invokes that event?

local OrbsFolder = game.Workspace:WaitForChild("OrbsFolder")
local plr = game.Players.LocalPlayer


for i, v in pairs(OrbsFolder:GetChildren()) do
	if v:IsA("Model") then
		v.ClickBox.ClickDetector.MouseClick:Connect(function()
			if game.Workspace.IsBreaking.Value == false and game.Workspace.CurrentBreak.Value == "nil" then
				local normName = v.Name
				
				v.Name = v.Name .. " Breaking"
				v.ClickBox.ClickDetector.MaxActivationDistance = 0
				game.Workspace.CurrentBreak.Value = v.Name
				game.Workspace.IsBreaking.Value = true
				
				local currentBreakName = v.Name

				game.ReplicatedStorage.Orb:FireServer(currentBreakName, normName)
			end
		end)
	end
end

Try removing the strings from the nil value. Just make it nil.

I want it to say nil and besides just doing nil don’t work because its a string value

i just need to know how to fix the for loop not picking up the changes

Considering that script.Parent:GetChildren() inside your first script is the same as OrbsFolder:GetChildren() in your second script, it should be working if you’ve confirmed it yourself that the item is in the folder.

Try comparing the values in your first script. Add print(v.Name, currentBreakName) right under the start of your for loop inside your first example and give us the results.

 CoinPile3, CoinPile3 Breaking

Is there only one object inside script.Parent:GetChildren()? If yes than there is the issue. There is nothing in that folder that matches with currentBreakName.

If that is the case, make sure that you are iterating through the same folder in your LocalScript that you are doing in your first script.

Nope there are many objects including CoinPile1, CoinPile2, CoinPile3, Crate

It just doesnt detect That ive changed the name to CoinPile3 Breaking

Those should then be included in your output if you haven’t provided the full output.

 CoinPile1, CoinPile3 Breaking
  CoinPile2 ,CoinPile3 Breaking
  CoinPile3 ,CoinPile3 Breaking
  Crate, CoinPile3 Breaking
  CoinPile1, CoinPile3 Breaking (x13)
  CoinPile2, CoinPile3 Breaking (x10)
  CoinPile3, CoinPile3 Breaking (x11)
  CoinPile2 ,CoinPile3 Breaking
  Crate ,CoinPile3 Breaking (x4)

You are changing the name on the client which does not replicate to the server. the name on the server will keep the same name while it’s different on the client. You are then sending the name which is only changed to the client to the server, thus why it’s not detecting the change.

How could I detect it from the client

Are you saying you want the name to be changed on the server aswell? Or do you want the for loop in your first example to be run on the client.

I would like to know if I could loop through the clients from the server and see if it changed on any of them

It’s possible, but it’s not really ideal as you would be relying on the client for information which has been known to cause many problems. Instead, you should be making it so the client is able to update the name on the server.

However I noticed something, the parameter “normName” contains the original name of the object. You could compare it with v.Name in your loop instead of currentBreakName and it should work.

If you are still looking to change the name of the object on the server, simply use a remote event to be fired from the client so the name can be updated on the server.