Main Script / Status Script

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.8 kB
2
Indexable
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local mapFolder = serverStorage.MapFolder

local minPlayers = 2
local tagDistance = 10
local gameLength = 60 -- in seconds

local maps = mapFolder:GetChildren()
local currentMap = maps[math.random(1,#maps)]

local waitingForPlayers = true
local gameStarted = false
local players = {}
local runners = {}
local tagger = nil
local startTime = 0

local function setTagger(newTagger)
    tagger = newTagger
    if tagger then
        tagger.Character.Head.BrickColor = BrickColor.new("Bright red")
        tagger.CharacterAdded:Connect(function(char)
            char.Head.BrickColor = BrickColor.new("Bright red")
        end)
        tagger.Character.Humanoid.Died:Connect(function()
            setTagger(nil)
        end)
        for i,runner in ipairs(runners) do
            runner.CharacterAdded:Connect(function(char)
                char.Head.ClickDetector.Parent = char.Head
            end)
            runner.Character.Head.ClickDetector.Parent = nil
        end
        tagger.Character.Head.ClickDetector.Parent = nil
    end
end

local function startGame()
    gameStarted = true
    waitingForPlayers = false
    startTime = tick()
    for i,player in ipairs(players) do
        local spawn = currentMap.SpawnPoint:Clone()
        spawn.Transparency = 1
        spawn.CanCollide = false
        player.Character:MoveTo(spawn.Position)
        if player ~= tagger then
            table.insert(runners, player)
            player.Character.Head.ClickDetector.MouseClick:Connect(function(clicker)
                if tagger and clicker.Parent == tagger.Character.Head then
                    setTagger(player)
                end
            end)
        end
    end
    setTagger(players[math.random(1,#players)])
end

game.Players.PlayerAdded:Connect(function(player)
    table.insert(players, player)
    if waitingForPlayers and #players >= minPlayers then
        replicatedStorage.Status.Value = "Starting game..."
        wait(2)
        startGame()
        replicatedStorage.Status.Value = "Game in progress!"
    else
        replicatedStorage.Status.Value = "Waiting for enough players to start the game..."
    end
end)

while not gameStarted do wait() end

while tick() - startTime < gameLength and #runners > 1 do
    wait(0.1)
end

if #runners == 1 then
    replicatedStorage.Status.Value = runners[1].Name .. " wins!"
else
    replicatedStorage.Status.Value = "Time's up! Runners win!"
end


-- Status Script(Seperate)

local Status = game:GetService("ReplicatedStorage"):WaitForChild("Status")

script.Parent.Text = Status.Value

Status:GetPropertyChangedSignal("Value"):Connect(function()
	
	script.Parent.Text = Status.Value
end)