-- Server Script for Round System, Voting, Tag System, and BillboardGui
-- Place this in ServerScriptService
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local voteEvent = Instance.new("RemoteEvent")
voteEvent.Name = "VoteEvent"
voteEvent.Parent = ReplicatedStorage
-- Define the available maps in ReplicatedStorage (adjust as needed)
local mapInstances = {ReplicatedStorage:WaitForChild("Map1"), ReplicatedStorage:WaitForChild("Map2"), ReplicatedStorage:WaitForChild("Map3")}
-- Global vote table and validation function
local voteTable = {}
local function isValidMap(mapName)
for _, map in pairs(mapInstances) do
if map.Name == mapName then
return true
end
end
return false
end
-- Connect the vote event once (handles voting for all rounds)
voteEvent.OnServerEvent:Connect(function(voter, mapName)
if not voteTable[voter] and isValidMap(mapName) then
voteTable[voter] = mapName -- Store the player's vote
end
end)
-- Function to make a player a tagger and handle tagging logic
local function makeTagger(player, updateBillboardFunc)
local character = player.Character
if character then
local isTaggerValue = character:FindFirstChild("IsTagger")
if not isTaggerValue then
isTaggerValue = Instance.new("BoolValue")
isTaggerValue.Name = "IsTagger"
isTaggerValue.Value = true
isTaggerValue.Parent = character
else
isTaggerValue.Value = true
end
-- Connect Touched event to the HumanoidRootPart for tagging
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
humanoidRootPart.Touched:Connect(function(hit)
local hitHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if hitHumanoid and hitHumanoid.Parent ~= character then -- Avoid self-collision
local otherPlayer = Players:GetPlayerFromCharacter(hit.Parent)
if otherPlayer and otherPlayer.Character:FindFirstChild("IsTagger") and not otherPlayer.Character.IsTagger.Value then
-- Tag the other player (make them a tagger)
makeTagger(otherPlayer, updateBillboardFunc)
-- Award 100 coins to the tagger who made the tag
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 100
-- Update the billboard to reflect new counts
updateBillboardFunc()
end
end
end)
end
end
-- Main round loop
while true do
-- Reset vote table at the start of each intermission
voteTable = {}
-- Intermission: 30 seconds for voting and preparation
print("Intermission started - Voting for map selection")
-- Show voting GUI to all players
for _, player in pairs(Players:GetPlayers()) do
local gui = ReplicatedStorage:WaitForChild("VotingGui"):Clone()
gui.Parent = player.PlayerGui
gui.Enabled = true
end
-- Wait for intermission duration
wait(30)
-- Hide voting GUI after intermission
for _, player in pairs(Players:GetPlayers()) do
local gui = player.PlayerGui:FindFirstChild("VotingGui")
if gui then
gui:Destroy() -- Or set gui.Enabled = false if you want to reuse it
end
end
-- Count votes and select map with the most votes
local mapVoteCount = {}
for _, map in pairs(mapInstances) do
mapVoteCount[map.Name] = 0
end
for _, votedMap in pairs(voteTable) do -- Iterate through player votes
if mapVoteCount[votedMap] then
mapVoteCount[votedMap] = mapVoteCount[votedMap] + 1
end
end
local maxCount = 0
local selectedMapName = mapInstances[1].Name -- Default to first map if no votes
for mapName, count in pairs(mapVoteCount) do
if count > maxCount then
maxCount = count
selectedMapName = mapName
end
end
-- Clone and place the selected map in the workspace
local selectedMap = ReplicatedStorage:FindFirstChild(selectedMapName):Clone()
selectedMap.Parent = workspace -- Assume maps are pre-positioned or anchored
-- Find spawn points on the map
local taggerSpawn = selectedMap:FindFirstChild("TaggerSpawn")
local runnerSpawnsFolder = selectedMap:FindFirstChild("RunnerSpawns")
if not taggerSpawn or not runnerSpawnsFolder then
warn("Spawn points missing on map " .. selectedMapName)
selectedMap:Destroy() -- Clean up and skip to next intermission
wait(5) -- Short delay before retrying
continue -- Skip to the next iteration of the loop
end
local runnerSpawnParts = runnerSpawnsFolder:GetChildren() -- List of runner spawn parts
-- Get all players and assign roles (one random tagger, rest runners)
local playerList = Players:GetPlayers()
if #playerList == 0 then
warn("No players in game, skipping round")
selectedMap:Destroy()
wait(5) -- Wait before retrying intermission
continue
end
local taggerIndex = math.random(1, #playerList)
local originalTaggerPlayer = playerList[taggerIndex]
table.remove(playerList, taggerIndex) -- Remove tagger from list, leaving only runners
-- Assign IsTagger value to all players and teleport them
for _, player in pairs(Players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local isTaggerValue = character:FindFirstChild("IsTagger")
if not isTaggerValue then
isTaggerValue = Instance.new("BoolValue")
isTaggerValue.Name = "IsTagger"
isTaggerValue.Parent = character
end
isTaggerValue.Value = (player == originalTaggerPlayer) -- Set tagger or runner role
end
-- Set up BillboardGui for displaying runner and tagger counts
local billboardPart = selectedMap:FindFirstChild("BillboardPart")
local updateBillboard
if billboardPart and billboardPart:FindFirstChild("BillboardGui") and billboardPart.BillboardGui:FindFirstChild("TextLabel") then
local textLabel = billboardPart.BillboardGui.TextLabel
updateBillboard = function()
local runnerCount = 0
local taggerCount = 0
for _, plr in pairs(Players:GetPlayers()) do
local char = plr.Character
if char and char:FindFirstChild("IsTagger") then
if char.IsTagger.Value then
taggerCount = taggerCount + 1
else
runnerCount = runnerCount + 1
end
end
end
textLabel.Text = "Runners: " .. runnerCount .. " Taggers: " .. taggerCount
end
else
updateBillboard = function() end -- Dummy function if BillboardGui is missing
end
-- Initial billboard update
updateBillboard()
-- Make the original tagger able to tag others
makeTagger(originalTaggerPlayer, updateBillboard)
-- Teleport players to their spawn points
originalTaggerPlayer.Character:SetPrimaryPartCFrame(taggerSpawn.CFrame) -- Tagger spawns at tagger location
for _, runnerPlayer in pairs(playerList) do
local randomSpawn = runnerSpawnParts[math.random(1, #runnerSpawnParts)] -- Random runner spawn
runnerPlayer.Character:SetPrimaryPartCFrame(randomSpawn.CFrame)
end
-- Start the 3-minute game timer in a separate thread
local gameEndEvent = Instance.new("BindableEvent")
spawn(function()
local endTime = tick() + 180 -- 3 minutes in seconds
local allTagged = false
while tick() < endTime and not allTagged do
wait(1) -- Check every second
local runnerCount = 0
for _, plr in pairs(Players:GetPlayers()) do
local char = plr.Character
if char and char:FindFirstChild("IsTagger") and not char.IsTagger.Value then
runnerCount = runnerCount + 1
end
end
if runnerCount == 0 then
allTagged = true -- All runners tagged
originalTaggerPlayer.leaderstats.Wins.Value = originalTaggerPlayer.leaderstats.Wins.Value + 1
end
end
-- Handle round end based on time or all tagged
if not allTagged then -- Time ran out
local runnerCount = 0
local remainingRunners = {}
for _, plr in pairs(Players:GetPlayers()) do
local char = plr.Character
if char and char:FindFirstChild("IsTagger") and not char.IsTagger.Value then
runnerCount = runnerCount + 1
table.insert(remainingRunners, plr)
end
end
if runnerCount > 0 then -- Runners win
for _, runner in pairs(remainingRunners) do
runner.leaderstats.Wins.Value = runner.leaderstats.Wins.Value + 1
runner.leaderstats.Coins.Value = runner.leaderstats.Coins.Value + 500
end
else -- All tagged (should be rare, but handle it)
originalTaggerPlayer.leaderstats.Wins.Value = originalTaggerPlayer.leaderstats.Wins.Value + 1
end
end
-- End the round: teleport back to lobby, destroy map, remove IsTagger
for _, plr in pairs(Players:GetPlayers()) do
if plr.Character then
plr.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(0, 50, 0) -- Lobby position (adjust as needed)
local isTagger = plr.Character:FindFirstChild("IsTagger")
if isTagger then
isTagger:Destroy() -- Remove for next round
end
end
end
selectedMap:Destroy() -- Remove the map from workspace
gameEndEvent:Fire() -- Signal that the game has ended
end)
-- Wait for the game to end before starting the next intermission
gameEndEvent.Event:Wait()
gameEndEvent:Destroy() -- Clean up the bindable event
end
-- Leaderstats setup for players (run when a player joins)
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0 -- You can add DataStore integration for persistence if needed
coins.Parent = leaderstats
end)