Hello, I am making a last man standing game and I have a table for the players ingame. But for some reason the players wont add. I have a few other bugs this leads to aswell so this is a big issue.
Here is my code:
wait(1)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local function restart()
script.Disabled = true
wait()
script.Disabled = false
end
local maps = {
ReplicatedStorage.ForestMap,
ReplicatedStorage.WorshipMap,
ReplicatedStorage.NeonMap
}
local gameStatus = ReplicatedStorage.gameStatus
while true do
--Waiting
gameStatus.Value = "Waiting for Players"
--repeat wait() until Players.NumPlayers > 1
--Intermission
gameStatus.Value = "Intermission, Time Left: "
local timeLeft = 15
for i = 1, timeLeft do
gameStatus.Value = ("Intermission, Time Left: " .. timeLeft)
wait(1)
timeLeft -= 1
end
--StartingGame
local chosenMap = maps[math.random(1, #maps)]
chosenMap.Parent = game.Workspace
local matchPlayers = {}
for i, v in pairs(Players:GetPlayers()) do
local Position = table.find(matchPlayers, v)
table.insert(matchPlayers, Position)
end
print(#matchPlayers)
gameStatus.Value = "Game In Progress"
for i, v in pairs(matchPlayers) do
v.Character:MoveTo(Vector3.new(math.random(-30, 30), 30, math.random(-30, 30)))
local sword = ReplicatedStorage["Wooden Sword"]:Clone()
sword.Parent = v.Backpack
end
--Game
while true do
wait()
for i, v in pairs(matchPlayers) do
v.Character.Humanoid.Died:Connect(function()
local position = table.find(matchPlayers, v)
table.remove(matchPlayers, v)
end)
Players.PlayerRemoving:Connect(function(v)
local position = table.find(matchPlayers, v)
table.remove(matchPlayers, v)
end)
end
end
--WinnerRewardAndGameEnd
if #matchPlayers == 1 then
print("game ended")
print(matchPlayers[1])
matchPlayers[1].leaderstats.Wins.Value += 1
print(matchPlayers[1].leaderstats.Wins.Value)
matchPlayers[1].Character.Humanoid.Health = 0
chosenMap.Parent = ReplicatedStorage
restart()
end
end
The restart thing is pointless, it stops the script and then the script won’t run anything else so it won’t reenable it, also use task.wait() as wait() is deprecated.
Try calling a bindableevent and attach another script to it and make that script restart it.
local matchPlayers = {}
for i, v in pairs(Players:GetPlayers()) do
local Position = table.find(matchPlayers, v)
table.insert(matchPlayers, Position)
end
to local matchPlayers = Players:GetChildren()
fix the issue?
The problem lies here, you’re trying to find the “Position” of a player in a table, where it has never been inserted, which would return nil, after that, you insert its “Position” into the table, but since it is nil, it won’t insert anything, leaving the table empty.
Simply rewrite your code to this and it should work. table.insert will automatically handle positioning of new entries into it, you don’t need to do it yourself. Cheers!
for i, v in pairs(Players:GetPlayers()) do
table.insert(matchPlayers, v)
end
If you want to remove a player from the list later on, what you can do is:
table.remove( matchPlayers, table.find( matchPlayers, player ) )
Although it doesn’t look fancy, this is a work around for utilizing tables this way. I suggest you try out dictionaries and you might get a better result, however I assure you this will work.