Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Would this script work for getting the winners of a round work?

Asked by 5 years ago
Edited 5 years ago

I'm not sure if I have any errors in this as I can't access studio right now, so I'm simply asking if someone could proof read it. The end goal for this script is to wait for the round to end, and when it does I want it to change the string value (Status) to the winners. The problem that I had to work around is that I want it to say "and x" when the last player name is being displayed, but I didn't want it to say and in between every player (example: a and b and c).

What I want it to look like is this: "The winners are: player 1, player 2, player 3, and player 4

    --// Variables \\--

    Status = game.ServerStorage.Status

    --// Main Script \\--
    game.ReplicatedStorage.RoundEnding.OnServerEvent:Connect(function(Reason)
        if Reason == “RunnersWon” then
            for i, player in pairs(game.Players:GetPlayers()) do
                wait(1.5)
                if player.Dead.Value ~= true and player.Role.Value ~= nil then
                    player.leaderstats.Points.Value = player.leaderstats.Points.Value+15                    
                    if i == 1 then
                        Status.Value = “The winners are: “ player.Name.. “,”
                    elseif i == NumPlayers then
                        Status.Value = Status.Value.. “ and ”.. player.Name
                    else
                        Status.Value = Status.Value.. player.Name.. “,”
                    end
                end
            end
        end
    end)

1 answer

Log in to vote
2
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

You need to add winners to a table, detect if table is not nil and use table.concat.

Wiki pages:

table.concat

for loop

Here is a example:

-- winners table
local winners = {}

-- Add players
for i,player in pairs(game.Players:GetPlayers()) do
    if player.Dead.Value ~= true and player.Role.Value ~= nil then
        table.insert(winners,v.Name)
    end
end

wait()

-- detect if table is nil or not

if winners[1] ~= nil then
    print("Winners: " .. table.concat(winners, ", ")) -- Print winners
else
    print("No winners")
end

here is your fixed script:

--// Variables \\--

local Status = game.ServerStorage.Status
local Winners = {}

--// Main Script \\--
game.ReplicatedStorage.RoundEnding.OnServerEvent:Connect(function(Reason)
    if Reason == "RunnersWon" then
        for i, player in pairs(game.Players:GetPlayers()) do
          if player.Dead.Value ~= true and player.Role.Value ~= nil then
            player.leaderstats.Points.Value = player.leaderstats.Points.Value+15
                table.insert(Winners,player.Name)
            end
        end
        wait()
        if Winners[1] ~= nil then
            Status.Value = "The winners are: " .. table.concat(Winners, ", ")
        else
            Status.Value = "No winners found!"
        end
    end
end)

Hope it helped :)

0
He hasn't checked it in a while, but this sure is helpful for me too. Currently trying to learn the basics of table.concat(). Mirzadaswag 110 — 5y
0
Thanks, my way did work but this script is definitely cleaner cmgtotalyawesome 1418 — 5y
Ad

Answer this question