Roblox Code

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.1 kB
15
Indexable
Never
local Tagger = nil
local MapFolder = game:GetService("ServerStorage"):WaitForChild("Maps")
local GameLength = 120
local ClickDetector = game.ServerStorage.ClickDetector:Clone()

-- Choose random map
local AvailableMaps = MapFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace

local SpawnPoint = ClonedMap:FindFirstChild("SpawnLocation")
-- Teleport all players to spawn location
local Players = game:GetService("Players"):GetPlayers()
for i, player in pairs(Players) do
	if player.Character and SpawnPoint then -- Check if the player has a character
		player.Character:MoveTo(SpawnPoint.Position)
	end
end

-- Choose random tagger
Tagger = Players[math.random(1,#Players)]

-- Give all players click detector except tagger
for i, player in pairs(Players) do
	if player ~= Tagger and player.Character then -- Check if the player has a character
		local newClickDetector = ClickDetector:Clone()
		newClickDetector.Parent = player.Character.HumanoidRootPart
		newClickDetector.Name = "ClickDetector"
	end
end

-- Define tagger's click detector function
local function onTaggerClick(otherPart)
	local otherPlayer = game.Players:GetPlayerFromCharacter(otherPart.Parent)
	if otherPlayer and otherPlayer ~= Tagger and otherPlayer.Character then -- Check if the other player has a character
		Tagger = otherPlayer
		print(Tagger.Name.." is now the tagger!")
		-- Destroy all old click detectors
		for i, player in pairs(Players) do
			local clickDetector = player.Character.HumanoidRootPart:FindFirstChild("ClickDetector")
			if clickDetector then
				clickDetector:Destroy()
			end
		end
		-- Give new tagger TNT head
		local TaggerHead = Tagger.Character.Head
		local Tnt = game.ServerStorage.Tnt:Clone()
		Tnt.Parent = TaggerHead
		Tnt.Position = TaggerHead.Position
		Tnt.CanCollide = false
		Tnt.Transparency = 1
		Tnt:WaitForChild("Handle").Transparency = 0
		-- Give all other players click detectors
		for i, player in pairs(Players) do
			if player ~= Tagger and player.Character then -- Check if the player has a character
				local newClickDetector = ClickDetector:Clone()
				newClickDetector.Parent = player.Character.HumanoidRootPart
				newClickDetector.Name = "ClickDetector"
			end
		end
	end
end

-- Connect tagger's click detector to function
local taggerClickDetector = Tagger.Character.HumanoidRootPart:FindFirstChild("ClickDetector")
if taggerClickDetector then
	taggerClickDetector.MouseClick:Connect(onTaggerClick)
end

-- Start game loop
local startTime = os.time()
repeat wait() until os.time() - startTime >= GameLength

-- Destroy all click detectors
for i, player in pairs(Players) do
	local clickDetector = player.Character.HumanoidRootPart:FindFirstChild("ClickDetector")
	if clickDetector then
		clickDetector:Destroy()
	end
end

-- Declare winners
local runners = {}
for i, player in pairs(Players) do
		if player ~= Tagger then
		table.insert(runners,player)
	end
end