Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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 :)

2 comments

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().

0

Thanks, my way did work but this script is definitely cleaner

This discussion is preserved as it was. New answers, comments, and votes are closed.