Right now, players can fall off any platform with no consequence. This section walks you through creating a large invisible hazard at the same height as the water in your game. When a player falls into the hazard, their health is reduced to zero and they respawn at the start of the game.
Create a basic water hazard
Create a basic water hazardIn Workspace > World, create a new folder named Hazards.Inside the Hazards folder, create a block Part named Hazard with Size 825, 1, 576 and CFrame.Position 174, -6.5, 38 so the part covers the water around the island and platforms.Set Transparency to 1 so the part is invisible and the actual water appears to be the hazard, disable CanCollide so players fall through it into the water, and enable Anchored so physics doesn't move it.
Detect when players touch the hazard
The hazard doesn't do anything until you write a script that listens Touched and lowers the player's health to zero.
Create HazardServiceIn ServerScriptService, create a Script named HazardService with the following code:```lualocal Players = game:GetService("Players")local Workspace = game:GetService("Workspace")local hazardsFolder = Workspace.World.Hazardslocal hazards = hazardsFolder:GetChildren()local function onHazardTouched(otherPart)local character = otherPart.Parentlocal player = Players:GetPlayerFromCharacter(character)if player thenlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenhumanoid.Health = 0endendendfor _, hazard in hazards dohazard.Touched:Connect(onHazardTouched)end```
The HazardService has many similarities to CoinService. However, instead of collecting a coin, the player has their health set to 0 when they touch a hazard.
Feel free to modify, add, or remove hazard objects in your game to create unique obstacles. As long as they are contained in the Hazards folder, the code loop connects the event handler to all of your hazards.
Connect to the player lifecycle
The player lifecycle represents events that occur when players interact in your game, such as joining, leaving, or respawning. You need to connect handlers to these events to execute logic for each major lifecycle event.
Add lifecycle handlers to CoinServiceIn ServerScriptService, append the following code to the bottom of the existing CoinService Script (don't replace any of the existing code — add this after it):```lualocal function onPlayerAdded(player)-- Reset player coins to 0updatePlayerCoins(player, function(_)return 0end)player.CharacterAdded:Connect(function(character)-- WaitForChild would stop the player loop, so below should be done in a separate threadtask.spawn(function()-- When a player diescharacter:WaitForChild("Humanoid").Died:Connect(function()-- Reset player coins to 0updatePlayerCoins(player, function(_)return 0end)end)end)end)end-- Initialize any players added before connecting to PlayerAdded eventfor _, player in Players:GetPlayers() doonPlayerAdded(player)endlocal function onPlayerRemoved(player)updatePlayerCoins(player, function(_)return nilend)endPlayers.PlayerAdded:Connect(onPlayerAdded)Players.PlayerRemoving:Connect(onPlayerRemoved)```
The code defines functions to reset coin counts during the appropriate lifecycle events:
- Players.PlayerAdded fires when a player joins the game, and sets the coin count to 0.
- Player.CharacterAdded fires when a player's character model is added to the world. It occurs after PlayerAdded and whenever the player respawns.
- Humanoid.Died fires when a player dies, and sets the coin count to 0. task.spawn() creates a separate thread for handling this, so other aspects of the player life cycle can execute.
- Player.PlayerRemoved fires when a player leaves the game to clean up player state.
- This code contains a potential issue where players could collect coins before the Players.PlayerAdded event executes and then have their coin counts reset to zero. To mitigate this issue, consider solutions such as code scheduling or freezing the player's character until initialization finishes. However, these solutions involve more complex scripting concepts that are beyond the scope of this tutorial.
Playtest
Select Test from the dropdown and click Play. Collect a few coins, then jump into the water. You character should die and respawn at the start, with their coin count reset to 0.