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

Trying to create a table of players but it only works once?

Asked by 4 years ago
Edited 4 years ago

So, I have achieved in which on touch it would add a player to a folder in which they would be a stringValue with their name in which I would know who to teleport to the main game.

For some reason, it's able to add but after removing the player once they have exited the platform, the script breaks.

  13:25:28.816 - Workspace.GameZone.GameZoneScript:47: attempt to index a nil value

and I get this. I know that means it can't find the player but how do I fix this?!

EDIT: fixed most problems, now it just won't loop.

local EnterBrick = script.parent.EnterZone
local PlayersToTeleport = {}
local tablesman = script.Parent.Lobby
local ExitBrick = script.Parent.ExitZone

EnterBrick.Touched:Connect(function(TouchedPart)
    numberOfElements = 0
        for key, value in pairs(PlayersToTeleport) do
         numberOfElements = numberOfElements + 1
        script.Parent.Screen.SurfaceGui.Frame.Players.Text = numberOfElements.."/6"
        end
    if TouchedPart.Parent:FindFirstChild("Humanoid") then
        local Character = TouchedPart.Parent
        local Player = game.Players:GetPlayerFromCharacter(Character)
        local AlreadyInTable = false
        for _,OtherPlayer in next,PlayersToTeleport do
            if OtherPlayer == Player then
                AlreadyInTable = true
            end
        end
        if not AlreadyInTable then
            print(Player)
        code = Instance.new("StringValue", tablesman)
        code.Name = Player.Name
        table.insert(PlayersToTeleport,Player)
        end
    end
end)

--[[ExitBrick.Touched:Connect(function(TouchedPart)
    if TouchedPart.Parent:FindFirstChild("Humanoid") then
        local Character = TouchedPart.Parent
        local Player = game.Players:GetPlayerFromCharacter(Character)
        tablesman:FindFirstChild(Player.Name):Destroy()
        table.remove(PlayersToTeleport,Player)
        end
end)]]

ExitBrick.TouchEnded:Connect(function(TouchedPart)
    if TouchedPart.Parent:FindFirstChild("Humanoid") then
        local Character = TouchedPart.Parent
        local Player = game.Players:GetPlayerFromCharacter(Character)
        local AlreadyInTable = true
        for _,OtherPlayer in next,PlayersToTeleport do
            if OtherPlayer == Player then
                AlreadyInTable = false
            end
        end
        if not AlreadyInTable then
            local remove = tablesman:FindFirstChild(Player.Name)
             if remove then
                print(Player.Name.."Removed")
                    for i, name in ipairs(PlayersToTeleport) do
             if name == Player.Name then
                 table.remove(PlayersToTeleport, i)
                 break
                 end
                end
                remove:Destroy()
         return remove 
          else
         return false
        end
        end
    end
end)
--script.Parent.Screen.SurfaceGui.Frame.Players.Text = numberOfElements.."/6"

0
TLDR; Ontouch function only runs once JackThePro202 -2 — 4y

Answer this question