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

My value isn't responding after the first reset?

Asked by 8 years ago
Edited 8 years ago

Both scripts are ServerScripts inside the ServerScriptService

Alright, so I designed two scripts, one that distributes rooms into the workspace, and the other placing the users within a room which is not occupied by another player. However my issue is the room assigning script placing the players within a unoccupied room. The script below. . .

local Table = {}
local Maxplr = nil
TableNumber = 1

function Assign(Player)
    if Table[TableNumber].Configuration.Occupied.Value ~= true then
        print(TableNumber.." Occupied By ".. Player.Name)
            Table[TableNumber].Configuration.Occupied.Value = true
            wait()
            Player.Character.Torso.CFrame = Table[TableNumber].CharacterSpot.CFrame*CFrame.new(0,2,0)       
    elseif TableNumber >= Maxplr then
    TableNumber = 1 
    Assign(Player)
    else
        TableNumber = TableNumber + 1
        print(TableNumber)  
        wait()
        Assign(Player)
    end
end

function PlayerAdded(Player)
repeat wait() until Player.Character ~= nil
        wait()
local GetSpawn= workspace.SpawnPoints:GetChildren() 
        for i,v in pairs(GetSpawn) do
            table.insert(Table,GetSpawn[i])
            Maxplr = i
        end

Player.CharacterAdded:connect(function()            
        Assign(Player)  
end)

Player.Character.Humanoid.Died:connect(function()
Table[TableNumber].Configuration.Occupied.Value = false 
end)

Assign(Player)
end

function PlayerRemoved(Player)
Table[TableNumber].Configuration.Occupied.Value = false
end

game.Players.PlayerAdded:connect(PlayerAdded)

game.Players.PlayerRemoving:connect(PlayerRemoved)

I have the print function on line 7, which tells me whom is being placed in what room. The Maxplr is max amount of rooms the game allows. For now, it's ten.

The first time, the script works 100% with the player spawning in their respective rooms.

1 Occupied By Player1

1 Occupied By Player1

2

2 Occupied By Player1

3

3 Occupied By Player1

Above is the prints I get after respawning each time. I have no idea in why it works the first reset and not the second, and so on. What I need to figure out is how to make sure it always returns them to the first unoccupied room, which should be 1. Help would be gladly appreciated!

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

I think that you are over complicating the problem. In this case it is much easier to simply keep a list of the players occupying each room and sending the newly joined player to first empty room.

Each room will be given a value from 1 up to the number of rooms. We will then initialize every value in 'occupied' array, which will be the same size as the number of room, to nil (not occupied). When a player joins the game we will find the first room that is not occupied (occupied[room] == nil) and then set the occupied value of that room to the player (occupied[room] = player) so it doesn't get assigned to someone else. When the character spawns, we teleport the character to the spawn location of that room.

When the player leaves the game, we simply search for them in the list and set the room's occupied value back to nil.

local occupied = {} -- List of players occupying each room
local spawns = game.Workspace.SpawnPoints:GetChildren()

for i = 1, #spawns do
    occupied[i] = nil -- fill with nil
end

local function find(value)
    for i = 1, #occupied do
        if occupied[i] == value then
            return i
        end
    end
    return 0
end

game.Players.PlayerAdded:connect(function(player)
    local room = find(nil)
    if room > 0 then
        occupied[room] = player
    else
        -- All rooms full
    end

    player.CharacterAdded:connect(function(character)
        player.Character.Torso.CFrame = spawns[room].CharacterSpot.CFrame*CFrame.new(0,2,0) 
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    local room= find(player)
    if room> 0 then
        occupied[room] = nil
    else
        -- Player not occupying a room
    end
end)
0
20:26:25.301 - Workspace.Auto Assign:29: attempt to index field '?' (a nil value) Dysplexus 50 — 8y
0
I fixed the end) issue on line 28, however I am getting that Workspace.Auto Assign:29: attempt to index field '?' (a nil value) Error. Dysplexus 50 — 8y
0
Yeah there were a couple of spelling mistakes (didn't test the code), are you still getting an error? BlackJPI 2658 — 8y
Ad

Answer this question