Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
TechModel 118
2 years ago

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://)
0
table.insert(a, b, c) "b" is expected to be a number or nil RAFA1608 543 — 2y
0
but it seems like the way you've done it is also valid, hmm RAFA1608 543 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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)
0
I needed them to be a string value. I did tostring(player.Name) but the error still occurs TechModel 118 — 2y
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. SuperLittleAdmin 257 — 2y
Ad

Answer this question