Hi,
So i was wondering how do i remove Players from a table? because when i do it, i expect it to remove the players from the table, but does nothing to the table, but continues to add them and gives some people more EXP, How do i fix this?
Note:
In This Script, there are comments to help you, to be more specific GetPlayers
and RemovePlayers
functions. They are marked as “Issue Code” with a Comment
Full Code
GD = require(game:GetService("ReplicatedStorage").GlobalDictionary)
Player = game:GetService("Players")
TweenService = game:GetService("TweenService")
ReplicatedStorage = game:GetService("ReplicatedStorage")
SoundService = game:GetService("SoundService")
Audio = workspace.Audio
ServerStorage = game:GetService("ServerStorage")
MapGui = game:GetService("StarterGui").MapGui.MainFrame
MapsStorage = ServerStorage.MapStorage
AlivePlayers = GD.AlivePlayers
IntermissionTime = ReplicatedStorage.Intermission.Value;
MaxTime = IntermissionTime * 10 -- I dont think i use this anymore
StatusValue = ReplicatedStorage.StatusValue
LoadTransparency = ReplicatedStorage.LoadTransparency
MusicVolume = ReplicatedStorage.MusicVolume
ColorValue = ReplicatedStorage.ColorValue
WaveValue = ReplicatedStorage.Wave
ActiveIntermission = true
RewardBonus = 1
Minutes = GD.WaveLengthInMinutes
Seconds = GD.WaveLengthInSeconds
local TeleportPos
local Maps = {
MapsStorage.Map1;
MapsStorage.Map2;
}
local function SetMap()
for _, map in pairs(MapsStorage:GetChildren()) do
local RM = Maps[math.random(1, #Maps)]
local A = RM:Clone() A.Parent = workspace.Map_Workspace
local MapName = A.Settings.MapName.Value
local Difficulty = GD.Difficulties[A.Settings.Difficulty.Value]
local Creator = A.Settings.Creator.Value
local DifficultyColor = GD.DifficultyColors[A.Settings.Difficulty.Value]
if A.Settings.Difficulty.Value > #GD.Difficulties then
warn("Difficulty Value Exceed's Max, Automatically Set to Extreme")
ColorValue.Value = GD.DifficultyColors[4]
StatusValue.Value = MapName.." ["..GD.Difficulties[4].."]: ".."by "..Creator
elseif A.Settings.Difficulty.Value < 1 or nil then
warn("Difficulty Value Lower than Minimum or nil, Automatically Set to Easy")
ColorValue.Value = GD.DifficultyColors[1]
StatusValue.Value = MapName.." ["..GD.Difficulties[1].."]: ".."by "..Creator
else
StatusValue.Value = MapName.." ["..Difficulty.."]: ".."by "..Creator
ColorValue.Value = DifficultyColor
end
print("Map Chosen: "..MapName)
ActiveIntermission = false
break
end
end
function GetPlayers() -- Issue Code 1
for _,Players in pairs(game.Players:GetPlayers()) do
table.insert(AlivePlayers, Players)
print("Players in game:",AlivePlayers) -- Just a test print
print(#AlivePlayers) -- same with this
Players.Character.Humanoid.Died:Connect(function()
table.remove(AlivePlayers)
print("died")
warn("Status Changed:",AlivePlayers)
print(#AlivePlayers)
end)
for _,Map in pairs(workspace.Map_Workspace:GetChildren()) do
TeleportPos = Map.SpawnPoint
if Map ~= nil then
Players.Character:MoveTo(TeleportPos.Position + Vector3.new(0,2,0))
end
end
end
end
function AwardPlayers()
for _,Player in pairs(AlivePlayers) do
Player:WaitForChild("SavedStats").EXP.Value += 50 * RewardBonus
end
end
function RemovePlayers() -- Issue Code 2
for _,Player in pairs(AlivePlayers) do
for _,Map in pairs(workspace.Map_Workspace:GetChildren()) do
Player.Character.Humanoid.Health = 100
TeleportPos = workspace.SpawnLocation
if Map ~= nil then
Player.Character:MoveTo(TeleportPos.Position + Vector3.new(0,2,0))
table.remove(AlivePlayers)
end
end
end
end
local function RemoveMap()
for i, Map in pairs(workspace.Map_Workspace:GetChildren()) do
if Map ~= nil then
Map:Destroy()
print("Map Destroyed")
break
end
end
end
while true do
local Timer = IntermissionTime
MaxTime = IntermissionTime * 10
for i = 1, IntermissionTime do
wait()
Timer -= 1
Audio.Main.Tick_Intermission:Play()
StatusValue.Value = "Intermission: "..Timer
end
wait(1)
if ActiveIntermission == true then
RemoveMap()
SetMap()
end
WaveValue.Value += 1
Minutes = GD.WaveLengthInMinutes
Seconds = GD.WaveLengthInSeconds
wait(5)
StatusValue.Value = "Game Starting"
wait(2)
LoadTransparency.Value = 0
ColorValue.Value = Color3.fromRGB(12,12,25)
wait(1.5)
MusicVolume.Value = 0.5
wait(1.5)
StatusValue.Value = "Wave "..WaveValue.Value..": "..GD.WaveLengthInMinutes..":00"
LoadTransparency.Value = 1
GetPlayers()
wait(1)
repeat wait(1)
if #AlivePlayers == 0 then
warn("Game Over, All Players Are Dead..")
StatusValue.Value = "Game Over"
ActiveIntermission = true
WaveValue.Value = 0
RewardBonus = 0
MusicVolume.Value = 0
LoadTransparency.Value = 0
wait(4)
RemovePlayers()
LoadTransparency.Value = 1
wait(1)
wait(5)
break
end
if Seconds <= 0 then
Minutes -= 1
Seconds = 59
else
Seconds -= 1
end
if Seconds <= 9 then
StatusValue.Value = "Wave "..WaveValue.Value..": "..tostring(Minutes)..":0"..tostring(Seconds)
else
StatusValue.Value = "Wave "..WaveValue.Value..": "..tostring(Minutes)..":"..tostring(Seconds)
end
until Minutes <= 0 and Seconds <= 0
wait(1)
if Minutes == 0 and Seconds == 0 then
warn("Game Over, Survivor's Win!")
StatusValue.Value = "Game Over"
AwardPlayers()
wait(3)
MusicVolume.Value = 0
LoadTransparency.Value = 0
RewardBonus += .2
wait(4)
RemovePlayers()
table.remove(AlivePlayers)
LoadTransparency.Value = 1
wait(1)
end
end
ModuleScript (In Case you are Wondering)
local GlobalDictionary = {
TweenService = game:GetService("TweenService");
Difficulties = {
[1] = "Easy";
[2] = "Medium";
[3] = "Hard";
[4] = "Extreme";
};
DifficultyColors = {
[1] = Color3.fromRGB(0,255,150);
[2] = Color3.fromRGB(208, 208, 0);
[3] = Color3.fromRGB(255,0,100);
[4] = Color3.fromRGB(200,0,0);
};
AlivePlayers = {};
WaveLengthInMinutes = 1; -- This is short due to testing
WaveLengthInSeconds = 0
}
return GlobalDictionary
To make your life easier:
Issue Code 1:
function GetPlayers() -- Issue Code 1
for _,Players in pairs(game.Players:GetPlayers()) do
table.insert(AlivePlayers, Players)
print("Players in game:",AlivePlayers) -- Just a test print
print(#AlivePlayers) -- same with this
Players.Character.Humanoid.Died:Connect(function()
table.remove(AlivePlayers)
print("died")
warn("Status Changed:",AlivePlayers)
print(#AlivePlayers)
end)
for _,Map in pairs(workspace.Map_Workspace:GetChildren()) do
TeleportPos = Map.SpawnPoint
if Map ~= nil then
Players.Character:MoveTo(TeleportPos.Position + Vector3.new(0,2,0))
end
end
end
end
Issue Code 2:
function RemovePlayers() -- Issue Code 2
for _,Player in pairs(AlivePlayers) do
for _,Map in pairs(workspace.Map_Workspace:GetChildren()) do
Player.Character.Humanoid.Health = 100
TeleportPos = workspace.SpawnLocation
if Map ~= nil then
Player.Character:MoveTo(TeleportPos.Position + Vector3.new(0,2,0))
table.remove(AlivePlayers)
end
end
end
end
Am i doing something wrong?
If you can help, that would be nice,
Thanks.