Reposted due to no answers after 4 weeks.
So, I have been trying to make a sword fighting typed game, with only one person doing it at a time. I have added a leaderstat value named "Status" to show when each player would take their turn (ex. Fighter 1, Fighter 2, Fighter 3, etc.) and I have a working script (will be provided) that can identify fighter 1 first, and fighter 2 second. Once they get identified, they just need to get teamed, and then another script takes over and runs them through the whole game. After a bit of time using that script, I noticed that some people do leave the game for whatever reason, and I wanted to add a fix that can jump from Fighter 1 to Fighter 3, so long as Fighter 2 no longer exists in the game. Every fix I have tried only seems to A) break with no problems in the output B) get Fighter 2 to go before Fighter 1 or C) somehow runs all players at the same time even though the game is only compatible for 1 player. Does anybody have a fix?
Note that this is my first project using active leaderboard scripting, and that any fixes you may have will need to be explained very simplified / broken down for me. Thanks!
Here is the working part of the script.
local players = game.Players:GetChildren() for i = 1,#players do local stats = players[i]:WaitForChild("leaderstats") local Status = stats:WaitForChild("Status") if players[i].stats.Status.Value == "Fighter "..game.Workspace.StatScript.Fighter.Value then -- This what the strategy I originally used. After the first player finishes, the value goes up by 1, and when the game restarts, the value goes back to 1. players[i].Team = game.Teams["Current Fighter"] script.Script.Disabled = false -- As long as the above command works and the player is teamed, this script being enabled should take care of the whole game. end end
If Fighter2 doesn't exist in the game anymore, why look for the next player in the list when you can move them down?
local Players = game:GetService('Players') local function UpdatePlayersAfter(Position: number, Difference: number | nil) -- set defaults if variable isn't set when the function is called if Difference == nil then Difference = -1 end -- loop through players, and get their current position for __index, Player in ipairs(Players:GetPlayers()) do local PlayerCurrentPosition = tonumber(Player.stats.Status.Value:gsub('%D', '')) -- get the current position as a number if Position < PlayerCurrentPosition then Player.stats.Status.Value = 'Fighter' + tostring(PlayerCurrentPosition-1) end end end Players.PlayerRemoving:Connect(function(Player) local LeavingPlayerStat = tonumber(Player.stats.Status.Value:gsub('%D', '')) -- get the current position as a number UpdatePlayersAfter(LeavingPlayerStat) end)