Im working on this script that has players names listed in a folder, and if they leave or join it adds or removes, but im adding a random check incase one slips by and theres an uneven ammount of players in the folder, but its coming back with an attempt to index number with name error?
while wait(1) do
local playchild = #(game.Players:GetChildren())
print(playchild)
local ingamechild = #(game.ReplicatedStorage:FindFirstChild("Ingame"):GetChildren())
print(ingamechild)
if playchild ~= ingamechild then
print("not equal")
for i = 1, playchild do
for i = 1, ingamechild do
if ingamechild.Name ~= playchild.Name then
ingamechild:Destroy()
end
end
end
end
end
ingamechild is a folder of stringvalue named and valued with the players username, and playchild is all the players, im trying to compare the list of real player names to the fake list of player names to check if someone not ingame is in the ingame folder
Why are you using value instances and folders in this case? Why not just use a modulescript? It’ll still be accessible by all other scripts and changes can be made easily without relying on the hierarchy.
Here’s what you can do: create a hash table documenting all of the player’s names. Why are we using hash tables? Because they’re much faster than finding the names in an array as you can simply just index for it.
local inGame: {[string]: boolean} = {}
--[[table is organized like this:
{
["randomPlayer123"] = true,
["anotherPlayer"] = true,
}
]]
for _, v: Player in game.Players:GetChildren() do
inGame[v.Name] = true
end
All you need to do now is iterate through the ingamechild table and compare each entry to the hash table. If the hash table doesn’t have the requested player, that’s how you know there’s a problem.
for _, v: StringValue in game:GetService('ReplicatedStorage').Ingame:GetChildren() do
if not inGame[v.Value] then --check to see if the player isn't in the game anymore
v:Destroy()
end
end
while wait(1) do
local playchild = #(game.Players:GetChildren())
print(playchild)
local ingamechild = #(game.ReplicatedStorage:FindFirstChild("Ingame"):GetChildren())
print(ingamechild)
if playchild ~= ingamechild then
print("not equal")
for i = 1, playchild do
for i = 1, ingamechild do
if ingamechild.Value ~= playchild.Value then
ingamechild:Destroy()
end
end
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local name = player.Name
if not game.ReplicatedStorage:FindFirstChild("Ingame"):FindFirstChild(name) then
local nametag = Instance.new("StringValue")
nametag.Value = name
nametag.Name = name
nametag.Parent = game.ReplicatedStorage:FindFirstChild("Ingame")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local name = player.Name
if game.ReplicatedStorage:FindFirstChild("Ingame"):FindFirstChild(name) then
game.ReplicatedStorage:FindFirstChild("Ingame"):FindFirstChild(name):Destroy()
end
end)
while wait(1) do
local playchild = #(game.Players:GetChildren())
print(playchild)
local ingamechild = #(game.ReplicatedStorage:FindFirstChild("Ingame"):GetChildren())
print(ingamechild)
if playchild ~= ingamechild then
print("not equal")
for i = 1, playchild do
for i = 1, ingamechild do
if ingamechild.Name ~= playchild.Name then
ingamechild:Destroy()
end
end
end
end
end
The # gets the total number of elements in an array, so when you do #(game.Players:GetChildren()) it will return how many players there are, not children, not an array. Remove the #.
Secondly iterating over children is done in a different style of for loop. Yours iterates over a number but you want to iterate over elements.
while task.wait(1) do
local playChildren = game.Players:GetChildren()
local ingameChildren = game.ReplicatedStorage:FindFirstChild("Ingame"):GetChildren()
-- when iterating we get index and element, we skip index with _
for _, player in playChildren do
for _, ingamechild in ingameChildren do
if ingamechild.Name ~= player.Name then
ingamechild:Destroy()
end
end
end
end