Using i, v in pairs() to loop through things

Hi, I’m wondering if anyone can help me. Basically I have this script which, once an event is fired, is supposed to loop through the things with the name I specified and delete them. It’s not working and I couldn’t seem to figure out why. When I click it, it deletes everything but what I want it to delete.

script.Parent.OnServerEvent:Connect(function(plr)
	for index, sign in pairs(game.Workspace:GetChildren(plr.Name.."Cordons")) do
		sign:Destroy()
	end
end)

I have a feeling I’m using :GetPlayer() wrong. Any help is appreciated because I’m quite stuck.

3 Likes

you don’t put anything inside the brackets in GetChildren(), instead check the name using string.sub to check if it has the name:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)  -- Dont know what you doing here
	for index, sign in pairs(game.Workspace:GetChildren()) do
		if string.sub(sign.Name,string.len(plr.Name) + 1) == "Cordons" then -- Don't know if it's string.len(plr.Name) or string.len(plr.Name)+1
           -- Do whatever
        end
	end
end)

Edit: Tested, there shouldn’t be a space or _, but if you want an underscore or space, put string.len() + 2 instead of 1

1 Like

Yes, GetChildren is being used wrong… might use FindFirstChild(plr.Name…“Cordons”) if all the cordons are in one model of the Workspace OR do a while loop removing them all till all models named this are destroyed or can’t be found… In such case, replace your loop with:

while game.Workspace:FindFirstChild(plr.Name.."Cordons") do
				game.Workspace:FindFirstChild(plr.Name.."Cordons"):Destroy()
		end
2 Likes

Thank you, I think I was over thinking it in my head. :smile:

1 Like

I do this too, sometimes :slight_smile:

That is not a really efficient way of doing it. There is a reason why people use another way more frequently.


The thing you’re doing wrong here is putting an argument in the parameter:

for index, sign in pairs(game.Workspace:GetChildren(plr.Name.."Cordons")) do

You can’t put things in those parameters, so this will fix it:

for index, sign in pairs(game.Workspace:GetChildren()) do

You will also have to have another if statement to see if the name is correct:

if sign.Name == plr.Name.."Cordons" then

This is the end result:

script.Parent.OnServerEvent:Connect(function(plr)
	for index, sign in pairs(game.Workspace:GetChildren()) do
        if sign.Name == plr.Name.."Cordons" then
		    sign:Destroy()
        end
	end
end)

why is it inefficient? its doing the same thing with less code…plus its not having to get every child of the workspace… I think getting every child of a workspace is less efficient, is it not?

The builtin shorthand for game.Workspace, workspace can be used to access workspace too.

The GetChildren() method doesn’t take any arguments, just returns an array of items under an object. If items exist under

workspace[plr.Name..'Cordons'] 

then you should’ve just done

workspace[plr.Name..'Cordons']:GetChildren()

and looped through that.

May I also ask why “for i,v in pairs”?
or do you really require the key from each key-value pair?

@JasonTheOwner

Generally, for loops are used over while loops for better readability.

This could work with Workspace and they key plr.Name…‘Cordons’, but I don’t know the design of his game, and he may have multiple instances (since Roblox allows duplicate names) of plr.Name…‘Cordons’… which is why I did a while loop, instead of assuming there was only one instance, named that… (probably how i would have designed it, setting that as the model, with all individual cordons placed within it).

So my answer was basically done in such a way that it could work in all situations for him - one instance or multiple objects under Workspace with the same name. :slight_smile:

game.Players.PlayerRemoving:Connect(function(Player)
	while workspace:FindFirstChild(Player.Name.."Cordons") do
    	workspace:FindFirstChild(Player.Name.."Cordons"):Destroy()
    end
end)

Hi, so I’ve done this but for when the player leaves, however it seems to be deleting every player’s cordon and not just their ones.

Oh wow, that’s odd. I would think it would only touch that player’s stuff with Player.Name…Cordons, in the game…

The only way I can see this happening is if the “Cordons” object (is/are) actually getting the player name attached to them, perhaps? Can you verify this on the server, to be sure?

Do you have multiple objects named Player.Name…“Cordons” or is it just one per players, off the Workspace?

I have checked server side and they are definitely called the player’s name and then “Cordons” . There are multiple objects named the same thing, that’s why I did the loop

1 Like

Hmm I’m not sure what else to do to fix this, but I’d do some debugging, like

print(Player.Name.."Cordons") 

and see what is being touched… maybe run studio in 2 player mode and test client and server.