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
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
1 comment
That destroys ALL of the values instead of just the player who left