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

How can I go through every child of an instance?

I'm making a player list where when a player is added a string value is created with the players name as the value, when a player leaves it is going to go through all the string values and compare it with game.Players. If it finds a player name in a string value and that player name is not in game.Players it will delete the string value which has that name. I was just wondering how you might do this and if it's even possible. Thanks!

game.Players.PlayerAdded:Connect(function(plr)
	local String = Instance.new("StringValue")
	String.Parent = game.ReplicatedStorage.Names
	String.Value = plr.Name
	local rs = game.ReplicatedStorage.Names:GetChildren()
	String.Name = #rs + 1
	game.Players.PlayerRemoving:Connect(function()
		-- This is where I will scan the values and players
	end)
	
end)

3 answers

Just destroy the string value; It's not out of scope because your PlayerRemoving listener is still inside the PlayerAdded listener:

game.Players.PlayerAdded:Connect(function(plr)
    local String = Instance.new("StringValue")
    String.Parent = game.ReplicatedStorage.Names
    String.Value = plr.Name
    local rs = game.ReplicatedStorage.Names:GetChildren()
    String.Name = #rs + 1
    game.Players.PlayerRemoving:Connect(function()
	    String:Destroy()
    end)
end)

1 comment

0

That destroys ALL of the values instead of just the player who left

How would I read that though, sorry I know this was not in my question but I have another local script reading the values and updating a text label of the players name, how would I update it so it deletes the text when it's number isn't there? I have about 8 of these.

local player = game.ReplicatedStorage:WaitForChild("Names"):WaitForChild("2")

script.Parent.Text = player.Value

player.Changed:Connect(function()

script.Parent.Text = player.Value

end)

I'm honestly very confused on what you are trying to achieve here and I'm sure there is a much better way to achieve what you are doing. First off all, I wouldn't recommend having the client having access to things where it can be avoided. Exploiters will have the ability to view into the Names as it is in a location which is replicated to the client. A nice spot I normally put things in would be ServerStorage.

Either way, I'll answer your question as appropriate but I would recommend storing this all into a module or just a array within your script as it will be easier to access through a script. If you are not familiar with Arrays you can read up on them here.

Iteration over a folder

The easiest way to solve your first question would be to just iterate over the folder whenever someone leaves. You can define the player in PlayerRemoving and you shouldn't really have it within the scope of the PlayerJoining as I see no reason in this case to have a event within a event.

Any who, once you've defined the player (same way you've done it for PlayerJoining) you are able to iterate over the folder using:

for Ind, Item in pairs(game.ReplicatedStorage.Names:GetChildren()) do
	-- This will run once for each value inside the Names folder
end

You can then use the Player argument you receive from PlayerRemoving to check if the Player Name exists within the folder and if it does then destroy it. This is probably the most convenient way to do something like this when using folders and StringValues. I'll leave the final code before if you require it:

game.Players.PlayerAdded:Connect(function(plr)
	local String = Instance.new("StringValue")
	String.Parent = game.ReplicatedStorage.Names
	String.Value = plr.Name
	local rs = game.ReplicatedStorage.Names:GetChildren()
	String.Name = #rs + 1
end)

game.Players.PlayerRemoving:Connect(function(plr)
	for Ind, Item in pairs(game.ReplicatedStorage.Names:GetChildren()) do
		if plr.Name == Item.Value then 
			Item:Destroy()
		end
	end
end)

Relying on the server

You are unable to make write to a value in ReplicatedStorage through a client script - you can only view them. In order to do this, you'd need to fire to the server however you'll need to be wary of doing this as exploiters are able to FireServer. This means if you did want to make a change to these, you'd need to run checks to make sure the person firing server is only altering there own however even this can lead to issues where exploiters can alter there own.

You haven't explained a lot to why it has to be done on the client and why it can't just be done through the server and then use FireClient(clienthere, varshere) to send whatever is required. Due to lack of information, I'm not going to provide any code but I'll provide you with these links if you wish to do some more research:

FireServer

FireClient

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