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

How could i remove a player from a Table when he died or disconnected?

Asked by 6 years ago
Edited 6 years ago

So this is kinda complicated to explain but im just gonna give you my whole Script so you might be able to understand what i did here. So i got a District System going like the Hunger games and i know how to check, how many Players are alive. But my major problem is to make a "Death/Leave" Script so it updates the Table when a player dies or disconnects. The Problem here is that If i want to check how many Players are alive i use

1if #Players == 0 then
2            print("Everybody died")
3            break
4        end

which is the only way i believe in this code. If i want to use _G.Districts it wont work. #Players gives me an Number of how many are in the table i believe.

So Is there a better way to write this script? Is there even a way to get my Death/Leave script working? Im so confused by now and my head hurts.

Here is my Whole Script + my attempt to write a Death and Leave Script. Basicly what i wanna know is how to make a death and leave script and make it comportable with mine. Thanks alot if you're still reading <3

DEATH AND LEAVE SCRIPT

01game:GetService("Players").PlayerAdded:Connect(function(player)
02    player.CharacterAdded:Connect(function(character)
03        character:WaitForChild("Humanoid").Died:Connect(function()
04        local Update = {}
05        for i, v in pairs(_G.Districts) do
06            if v ~= player.Name then
07               table.insert(Update,1,v)
08            end
09        end
10    _G.Districts = Update
11        end)
12    end)
13end)
14 
15game:GetService("Players").PlayerRemoving:Connect(function(player)
View all 25 lines...

MAIN SCRIPT

01--//Services
02local Players = game:GetService("Players")
03local ReplicatedStorage = game:GetService("ReplicatedStorage")
04 
05--//Remotes
06local Remotes = ReplicatedStorage:WaitForChild("Remotes")
07    local Names = Remotes:WaitForChild("Names")
08    local Round = Remotes:WaitForChild("Round")
09 
10--//Variables
11local Tributspawn = workspace:WaitForChild("Tribut")
12 
13--//Functions
14    function Shuffle(Table)
15    local Size = (#Table)
View all 48 lines...

1 answer

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

You should avoid using _G as any scripts are able to read / make changes to this. Instead I would use a module script to manage this task.

There are a lot of ways this can be done but for this example I will show a self contained process which will return a round object. The idea behind this is so you would then be able to expand the functionality of what a round is.

01-- module script code
02local f = {}
03local plrServ = game:GetService("Players")
04 
05-- vars
06local curRound = nil
07 
08-- returns the cur round object or nil
09function f:GetCurRound()
10    return curRound
11end
12 
13-- roundTime, the time for this round
14function f:NewRound(roundTime)
15    -- example validation for rounds
View all 94 lines...

Example script

01local round = require(script.Parent.ModuleScript)
02 
03while true do
04    local newRound = round:NewRound(10)
05 
06    while newRound:IsActive() do
07        print(newRound:GetPlayerCount(), "Players in round")
08        wait(1)
09    end
10 
11    print("round end")
12    newRound:End()
13end

I know this is a lot to work with at first but the round object returned is safe to use in other scripts. Please comment if there are parts of this code you do not understand and I will add additional info below.

Usefull links

OOP in Lua

OOP in Lua dev forum

RBXScriptConnection

Hope this helps.

0
I see you did a lot of work there, sadly my knowledge is not good enough for this kind of code. I never invented time into ModuleScripts etc also sadly i dont know how to make it comportable with mine. Still you did a great job, i tested it in a new game and it works well :) Paintertable 171 — 6y
0
You can do a lot of things with modules. A module script will only run once upon using the function require. It helps lead to better code design by reusing code and making for better code strucures ie one thing does one task. User#5423 17 — 6y
Ad

Answer this question