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

Remove PlayerRemoving off the table and PlayerAdded in, requires number not variable?

Apparently to add and remove players off the xf list of mine, i need to call the numbers but I don't know that, but by doing removing the table if the name of the value is caught. the error log says number expected not tostring. What can I do? Thanks

local xf = {
        "...",
    }

    for i, v in pairs(game:GetService("Players"):GetPlayers()) do
        if v ~= game:GetService("Players").LocalPlayer then
            table.insert(xf, tostring(v.DisplayName))
        end
    end

    game.Players.PlayerAdded:Connect(function(v)
        if not table.find(xf, tostring(v.DisplayName)) then
            table.insert(xf, tostring(v.DisplayName))
        end
    end)

    game.Players.PlayerRemoving:Connect(function(v)
        if table.find(xf, tostring(v.DisplayName)) then
            table.remove(xf, tostring(v.DisplayName))
        end
    end)[](http://)

3 comments

0

table.insert(a, b, c) "b" is expected to be a number or nil

0

but it seems like the way you've done it is also valid, hmm

1 answer

You can try to make it a dictionary instead if it does not conflict with your code.

local Players = game:GetService('Players')
local dictionary = {}

for _, player in pairs(Players:GetPlayers()) do
	if player ~= Players.LocalPlayer then
		dictionary[player.Name] = true
	end
end

Players.PlayerAdded:Connect(function(player)
	dictionary[player.Name] = true
end)

Players.PlayerRemoving:Connect(function(player)
	dictionary[player.Name] = nil
end)

OR

local Players = game:GetService('Players')
local array = {}

for _, player in pairs(Players:GetPlayers()) do
	if player ~= Players.LocalPlayer then
		table.insert(array, player.Name)
	end
end

Players.PlayerAdded:Connect(function(player)
	table.insert(array, player.Name)
end)

Players.PlayerRemoving:Connect(function(player)
	table.remove(array, table.find(array, player.Name))
end)

2 comments

0

I needed them to be a string value. I did tostring(player.Name) but the error still occurs

0

Well yea, you cannot read off an array directly with string. However, I just remembered that table.find returns the index, so you can use that instead. I’ll modify the answer.

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