Menu
CoddyTech

Water That Defeats

Lesson 10 of 20 in Coddy's Roblox Game: Disaster Survival course.

Time for the first disaster. The game will have several, so they go in one table, each a function that takes the map. A function stored in a table is called like any other:

local Disasters = {}

function Disasters.Flood(map)
	-- make the water
end

Disasters.Flood(map)

The flood starts as a wide blue part under the island. It must not hold players up, so CanCollide is false (Touched still fires), and it stays Anchored. Every disaster will defeat whoever it touches, so that job gets a name:

local function defeat(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

lava.Touched:Connect(defeat)

hit is the part that touched, such as a foot; its Parent is the character. Parent the water to the map, so destroying the map at the end of the round takes the water with it.

challenge icon

Challenge

Medium

Add the first disaster: water that defeats.

  1. Write defeat(hit): if hit.Parent has a Humanoid, set its Health to 0.
  2. Make local Disasters = {} and Disasters.Flood(map): a part Water in the map, anchored, CanCollide false, 10 studs wider and longer than map.Base, 2 studs below it, Touched connected to defeat.
  3. Call Disasters.Flood(map) after the teleport.

Press Play and walk off the island: the water under it defeats you. Then Check.

Try it yourself

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local lobbySpawn = workspace.LobbySpawn

print("Rounds last " .. Settings.RoundLength .. " seconds")

-- Every player gets Wins on the leaderboard
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.Parent = leaderstats
end)

-- The status board in the lobby
local gui = Instance.new("SurfaceGui")
gui.Parent = workspace.StatusBoard

local status = Instance.new("TextLabel")
status.Name = "Status"
status.Size = UDim2.fromScale(1, 1)
status.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
status.TextColor3 = Color3.fromRGB(255, 255, 255)
status.TextScaled = true
status.Text = "Welcome to Disaster Survival!"
status.Parent = gui

local function setStatus(text)
	status.Text = text
end

local function teleport(player, target)
	local character = player.Character
	if character then
		character:PivotTo(target.CFrame + Vector3.new(0, 4, 0))
	end
end

-- Lesson 10: Water That Defeats goes here

-- The round loop
while true do
	for seconds = Settings.Intermission, 1, -1 do
		setStatus("Intermission: " .. seconds)
		task.wait(1)
	end

	local map = ServerStorage.Maps.Island:Clone()
	map.Name = "Map"
	map.Parent = workspace

	local survivors = {}
	for _, player in Players:GetPlayers() do
		local character = player.Character
		if character and character.Humanoid.Health > 0 then
			teleport(player, map.Spawn)
			survivors[player] = true
			character.Humanoid.Died:Connect(function()
				survivors[player] = nil
			end)
		end
	end

	for seconds = Settings.RoundLength, 1, -1 do
		setStatus("Time left: " .. seconds)
		task.wait(1)
	end

	local names = {}
	for player in survivors do
		player.leaderstats.Wins.Value += 1
		teleport(player, lobbySpawn)
		table.insert(names, player.Name)
	end
	map:Destroy()
	if #names > 0 then
		setStatus("Winners: " .. table.concat(names, ", "))
	else
		setStatus("Nobody survived!")
	end
	task.wait(3)
end

All lessons in Roblox Game: Disaster Survival

Practice on your own: Roblox playground