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