Rising Flood
Lesson 10 of 18 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 and raise it
end
Disasters.Flood(map)The flood is a wide blue part that starts under the island and rises, but not to the top: the rocks on the Island and the roofs on Rooftops stay dry, so a player who climbs in time survives. A disaster nobody can survive is no game. TweenService changes a property smoothly over time: describe how long with a TweenInfo, give the goal values, and Play it.
local rise = TweenInfo.new(12, Enum.EasingStyle.Linear)
TweenService:Create(water, rise, { Position = water.Position + Vector3.new(0, 8, 0) }):Play()The water must not hold players up, so CanCollide is false and it stays Anchored. Defeating whoever touches it is the same job lava did in the first course; this game gives it a name, defeat(hit), because every disaster will use it.
Parent the water to the map, so destroying the map at the end of the round takes the water with it.
Challenge
MediumAdd the flood:
- Get
TweenServiceat the top. - Write
defeat(hit): ifhit.Parenthas a Humanoid, set its Health to 0. - Make
local Disasters = {}andfunction Disasters.Flood(map): a part namedWaterin the map, anchored, not CanCollide, blue and see-through (Transparency 0.3), 10 studs wider thanmap.Baseeach way, starting 2 studs below it. Touching it defeats. Tween its Position 8 studs up overSettings.RoundLength * 0.8seconds, at a steady pace (Enum.EasingStyle.Linear), so it stops below the tops of the rocks. - Call
Disasters.Flood(map)right after the players are teleported.
Press Play and race the tide: jump up the rocks to the left of the spawn. 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
-- 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.