Ad
Log in to vote
0

How do you identify if a certain leaderstat exists?

Asked by 19 days ago
Edited 19 days ago

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

0
I don't see the problem, you said it was the working part, can you post the part that doesnt work? Also, why dont you use ''for i, v in pairs(game.Players:GetChildren())" Kingu_Criminal 111 — 19d
0
I had given that a try before this script, and nothing was coming out of it. This script I have provided has the most working history, but anytime I add an elseif to line 5 saying that fighter value doesn't exist, it crashes and burns. With that being said, I have no idea which method of elseif would be the most efficient if working. Would you happen to have a solution? taxicar24 19 — 19d

1 answer

Log in to vote
0
Answered by 12 days ago

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)
0
Came up with the issue "stats is not a valid member of Player "Players.taxicar24" on line 18. taxicar24 19 — 10d
0
What you could try do instead is saving the Player's Status value in an array and replace LeavingPlayerStat with the player's previous stats.Status value ???? loowa_yawn 261 — 8d
0
No idea how. Again, I am new to leaderstat scripts, and would just need a negative statement to add onto the first script. taxicar24 19 — 8d
Ad

Answer this question