How to make 3 players win instead of 1

So i made this code for a minigame, when u touch the winnerpart the first player that touches it wins. so how would i be able to make that 3 players are able to win, instead of 1 and also have the names correctly displayed in the statusbar?. Ive been thinking for a while and i really need a second brain because im stuck :sweat_smile:

local roundLenght = 60
		local canWin = true
		local roundType = ""

		if map:FindFirstChild("Race") then
			roundType = "Race"
			map.WinnerPart.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and canWin == true then
					canWin = false
					status.Value = hit.Parent.Name.." Has won!"

					local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
					plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value +1
					plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value +100
				end
			end)	
		end

		repeat
			roundLenght = roundLenght -1
			status.Value = "Time left: "..roundLenght
			wait(1)
		until roundLenght == 0 or canWin == false or #workspace.Ingame:GetChildren() == 0

Hey there,

I think something you could do is create an array with the winners of the minigame. When a player touches the specific part, insert their player instance into the array. Check how many instances are in the array, and when the length is equal to 3, you should have all three player instances. At this point you can do whatever you’d like with them, for example get their player name.

Let me know if you would like me to help explain this inside of a script, and I can create some sample code.

That would be nice, because at the moment i only know how to create an instance with Instance.new

Im just a few months into scripting so im just stuck on this part and some explanation code would be appreciated!.

Alright, that is okay. I will write a script for you, one second.

Hopefully this helps:

local roundLenght = 60
local canWin = true
local roundType = ""

local winners = {}

if map:FindFirstChild("Race") then
	roundType = "Race"
	map.WinnerPart.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and canWin == true then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			
			local inTable = false
			for i,v in pairs(winners) do
				if v == plr then
					inTable = true
				end
			end
			
			if inTable == false then
				table.insert(winners,plr)
			end
			
			if #winners >= 3 then
				canWin = false
				
				status.Value = winners[1].Name ..", ".. winners[2].Name .." and ".. winners[3].Name .." have won!"
				
				for i,v in pairs(winners) do
					v.leaderstats.Wins.Value += 1
					v.leaderstats.Cash.Value += 100
					
					table.remove(winners,i)
				end
			end
		end
	end)	
end

repeat
	roundLenght = roundLenght -1
	status.Value = "Time left: "..roundLenght
	wait(1)
until roundLenght == 0 or canWin == false or #workspace.Ingame:GetChildren() == 0
local winners = {}
local roundLenght = 60
		local canWin = true
		local roundType = ""

		if map:FindFirstChild("Race") then
			roundType = "Race"
			map.WinnerPart.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and canWin == true then
                   if #winners <= 3 then -- checks amount of values in the table
                    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
                    if not table.find(winners,plr.Name) then
                    table.insert(winners,plr.Name)
					status.Value = hit.Parent.Name.." Has won!"
					plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value +1
					plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value +100
                    local String = table.concat(winners,",")
                    status.Value = String.." Won!"
                    end
                    else
                    canWin = false
                    table.clear(winners)
                      end
                       end
			end)	
		end

		repeat
			roundLenght = roundLenght -1
			status.Value = "Time left: "..roundLenght
			wait(1)
		until roundLenght == 0 or canWin == false or #workspace.Ingame:GetChildren() == 0

Sorry for the late reply, but now nothing happens when i touch the winnerpart. it gives no errors

try mine.

Char Limit

If you send the errors we may be able to help work through them. It was hard to test, because I would need to create a map and stuff.


did i make a typo?

sorry i really dont know how to identify these error

show me the full script.
[Char Limits]

Yours gave no errors but nothing happened when i touched the part

local status = game.ReplicatedStorage.Status
local maps = game.ReplicatedStorage.Maps:GetChildren()

while true do
	status.Value = "Waiting For Players"
	repeat task.wait() status.Value = "Waiting for a player to join" until #game.Players:GetPlayers() >= 1
	if #game.Players:GetPlayers() >= 1 then
		status.Value = "Starting"
		for i = 1,15 do
			status.Value = "Next round will start in: "..15-i
			wait(1)
		end

		local rand = math.random(1, #maps)

		local map = maps[rand]:Clone()
		map.Parent = workspace

		status.Value = "The next minigame is: "..map.Name
		wait(5)


		local players = game.Players:GetChildren()
		for i = 1,#players do
			if players[i].Character ~= nil then
				local spawnLocation = math.random(1,#map.Teleports:GetChildren())
				players[i].Character:MoveTo(map.Teleports:GetChildren()[spawnLocation].Position)
				players[i].Character.Parent = workspace.Ingame

			end
		end

		local roundLenght = 60
		local canWin = true
		local roundType = ""
		local winners = {}

		if map:FindFirstChild("Race") then
			roundType = "Race"
			map.WinnerPart.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and canWin == true then
					if #winners <= 3 then -- checks amount of values in the table
						local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
						table.insert(winners,plr)
						status.Value = hit.Parent.Name.." Has won!"
						plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value +1
						plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value +100
						local String = table.concat(winners,",")
						status.Value = String.." Won!"
					else
						canWin = false
						table.clear(winners)
				end
			end)	
		end

		repeat
			roundLenght = roundLenght -1
			status.Value = "Time left: "..roundLenght
			wait(1)
		until roundLenght == 0 or canWin == false or #workspace.Ingame:GetChildren() == 0

		wait(3)
		map:Destroy()	

		local players = game.Players:GetChildren()
		for i = 1,#players do
			if players[i].Character ~= nil then
				players[i]:LoadCharacter()
			end
		end
	end
end

Ok so I did end up rewriting the whole thing but here you go:
For this to work should put a stringvalue in repstorage
All of this goes in the serverscriptstorage

local roundLenght = 60
local canWin = true
local roundType = ""
local Winners = game:GetService("ReplicatedStorage").Values:FindFirstChild("Winner") --whatever the value that stores winners name is
local win = {} -- using a table to show the winners is better in case of multiple winners
local connection
if map:FindFirstChild("Race") then
	roundType = "Race"
	connection = map.WinnerPart.Touched:Connect(function(hit) --put whatever the winbrick is there
		if hit.Parent:FindFirstChild("Humanoid") and canWin == true then
			if table.find(win, game:GetService("Players"):FindFirstChild(hit.Parent.Name)) then return end -- finds if player already touched the brick. If so then doesnt add in table again
			table.insert(win, game.Players:FindFirstChild(hit.Parent.Name)) -- inserts name in table
			print(hit.Parent.Name) -- prints player name 
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- the leaderstat change stuff
			plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value +1
			plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value +100
		end
		if #win >= 3 then connection:Disconnect() end -- stops the thing from running again if there are 3 winners already
	end)
	repeat
		roundLenght = roundLenght -1
		status.Value = "Time left: "..roundLenght
		wait(1)
	until roundLenght == 0 or canWin == false or #workspace.Ingame:GetChildren() == 0
	-- when round ends:
    Winners.Value = "The winner is "
	for i, v in pairs(win) do
		Winners.Value = Winners.Value .. ", " .. win[i].Name -- basically the winners value is "The winner is". It adds the winners names onto it.
		-- if there are 3 winners then: it will be like this: The winner is, elomala, cooldude123, funniman987
	end
	task.wait(0.5)
	Winners.Value = "" -- This wont trigger the gui to show as
end

And for the GUI:
Put as a localscript inside the textlabel

local Winners = game:GetService("ReplicatedStorage").Values:FindFirstChild("Winner") -- the value which stores what the winner names to be displayed
local gui = script.Parent -- the gui that displays winners (advised to put the script inside textbox)

Winners.Changed:Connect(function() -- when the value that stores names of winners is changed
	if Winners.Value == "" then return end -- If the value is nothing then no run
	gui.Text = Winners.Value
	gui.Visible = false
	wait(5)
	gui.Visible = true
end)

By the way, I am also kind of new to scripting so this might not be the best possible way. Goodluck though.

you have to add an end before end) which is in line 54.

Now it says this and it gives me 400 points and 4 wins so 4 times of everything

It says i have won though

fixed it also added a check to check if the player has already won.

Thanks for writing all of this although when i try to use it i get this error in return?

1 Like

Oh I’m sorry
Just remove the “.Values”
from this part:

local Winners = game:GetService("ReplicatedStorage").Values:FindFirstChild("Winner") -- remove the .Values written after the getservice thing

Also, do the same in the serverscript where the Winners variable is defined.
Also don’t forget to add a string value named “Winner”.

No errors now and it indeed says i have won, ill have to test it with a friend tho if it also works with 2 players :slight_smile: so give me a few minutes

1 Like