Attempt to index number with name?

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

Blockquote

1 Like

Is it not obvious to you?

You defined playchild as a number. The same goes with ingamechild.

And then you tried indexing the same variable.

You might want to explain your goal a bit better, it’s hard to figure out what exactly it is that you want to do

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

you don’t have to put play child and ingamechild in parentheses

local playchild = #game.Players:GetChildren()
local ingamechild = #game.ReplicatedStorage:FindFirstChild("Ingame"):GetChildren()
for i,v in pairs(ingamechild:GetChildren()) do
if game.ReplicatedStorage.Ingame:FindFirstChild(v.StringValue.Value) then
v:Destroy()
end

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

Instead of .Name put .Value

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

This doesnt give me an error but it doesnt remove anything if i put a random thing in the folder

show me yo script i have no idea what ure doing with the sample code

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

is this the right code? it looks the exact same

i think he meant to show us his full code.

its different? it what i got in my whole script

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
1 Like