Title explains issue. All help is appreciated!
local Players = game:GetService("Players")
local playerTable = {}
local function onPlayerDied(player)
print(player.Name .. " has died")
if playerTable[player.Name] then
playerTable[player.Name]["isAlive"] = false
end
end
local function startNewRound()
-- Clear the table at the start of a new round
playerTable = {}
-- Re-add all players to the table
print("Starting")
for _, player in ipairs(Players:GetPlayers()) do
playerTable[player.Name] = {
["isAlive"] = true
}
end
end
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local survivals = Instance.new("IntValue")
survivals.Name = "Survivals"
survivals.Value = 0
survivals.Parent = leaderstats
-- Load player's data from the data store
local userId = player.UserId
-- Attempt to retrieve the player's survival data from the data store
local success, data = pcall(function()
return playerDataStore:GetAsync(userId)
end)
if success then
if type(data) == "table" and data.survivalStats then
survivals.Value = data.survivalStats
else
survivals.Value = 0
end
else
warn("Failed to retrieve survival data for player " .. player.Name)
end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerDied)
Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
char:FindFirstChild("Humanoid").Died:Connect(function()
onPlayerDied(plr)
end)
end)
end)
local function endRound()
print("Ended")
for _, player in ipairs(Players:GetPlayers()) do
if playerTable[player.Name] and playerTable[player.Name]["isAlive"] then
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local survivals = leaderstats:FindFirstChild("Survivals")
if survivals then
survivals.Value = survivals.Value + 1
print("Added a survival")
end
end
end
end
end
startNewRound()
local Gamemodes = {
["Gamemode1"] = 0.2,
}
local Weight = 0
for _, Chance in pairs(Gamemodes) do
Weight += (Chance * 10)
end
local ranNumber = math.random(1, Weight)
Weight = 0
for Gamemode, Chance in pairs(Gamemodes) do
Weight += (Chance * 10)
if Weight >= ranNumber then
print('The gamemode is '..Gamemode)
Gamemodes = game.Lighting.Gamemodes
Gamemode = Gamemodes[Gamemode]:Clone()
if Gamemode.Name == "Gamemode1" then
--gamemode code
endround()
end