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

Extra value is added to a table?

Asked by 3 years ago
Edited 3 years ago

So, this little snippet of code is supposed to detect the player within a radius of 30 studs of a part and take their teamcolor and put it in a table called 'colors,' and if a different player comes in with and has a different color for teamcolor it puts it in another table called 'color2.' This works wonderfully, except that it keeps the different teamcolor in with the table with the original teamcolor.

Code:

while true do
    colors = {}
    color2 = {}
    for i, plr in ipairs(game.Players:GetChildren()) do
        local char = plr.Character
        local plr = game.Players:GetChildren()
        local plrr = game:GetService("Players")
        if char ~= nil then
            local t = char:FindFirstChild("Torso")
            local h = char:FindFirstChild("Humanoid")
            if t ~= nil and h ~= nil then
                if h.Health > 0 then
                    local pos1 = t.Position * Vector3.new(1, 0, 1)
                    local pos2 = cappart.Position * Vector3.new(1, 0, 1)
                    if (pos1 - pos2).magnitude < radius then
                        local player = plrr:GetPlayerFromCharacter(char)
                        local clrfound = findcolor(plr.TeamColor)
                        local colorf = player.TeamColor
                        if colorf ~= nil then
                            table.insert(colors,{colorf, 1})
                            tcp.BrickColor = player.TeamColor
                        end
                        if colorf ~= nil then
                            if colorf ~= colors[1][1] then
                                table.insert(color2,{colorf, 1})
                            end
                        end
                    end
                end
            end
        end
    end

Not that much. (I have it constantly printing both tables) (and yes there is a wait thing but it is all the way at the bottom and I am too lazy to scroll all the way down) Output:

{
                    [1] =  ?  {
                       [1] = Cocoa,
                       [2] = 1
                    }

^ is the first table when a player is in works wonderfully table 2 (color2) has nothing inside perfect but when another player of a different team comes along, this is the output:

{...} 1 
     ?  {
                    [1] =  ?  {
                       [1] = Cocoa,
                       [2] = 1
                    },
                    [2] =  ?  {
                       [1] = Storm blue,
                       [2] = 1
                    }

^ above is the original colors table uh oh thats not good it now includes the second teamcolor, storm blue but the table for color2 is fine, just has storm blue output for table 2:

 {
                    [1] =  ?  {
                       [1] = Storm blue,
                       [2] = 1
                    }
                 } 1

^ ah yes, good good

The only trouble here is that the first table includes the second teamcolor when a player of a different team comes into the radius, and it shouldn't.

How would I be able to fix this?

1 answer

Log in to vote
0
Answered by 3 years ago

Right so, I should have used table.find function to find the different color and remove it. Works now. I am small brain.

Yep.

if colorf ~= nil then
    if colorf ~= colors[1][1] then
        table.insert(color2,{colorf, 1})
        if colors[2][2] ~= nil == true then
        local notcool = table.find(colors, colors[1][1])
        table.remove(colors, notcool)
Ad

Answer this question