The Flashlight
Lesson 2 of 25 in Coddy's Roblox Game: Escape the Dark course.
In the dark you need a flashlight. It is a Tool in ServerStorage, and its Handle holds a SpotLight named Beam: a cone of light out of one face of the part, the front of the flashlight.
A player's Backpack is emptied every time they spawn, so the flashlight is handed out on each CharacterAdded. A click with a tool in hand fires its Activated event, on the server too, and that switches the beam:
flashlight.Activated:Connect(function()
beam.Enabled = not beam.Enabled
end)Press 1 to hold it, and click to switch it on.
Challenge
EasyGive every player a flashlight that clicks on and off.
- In
Players.PlayerAdded, on everyplayer.CharacterAdded, cloneServerStorage.Flashlightintoplayer.Backpack. - Connect the flashlight's
Activatedto switchHandle.Beam.Enabled.
Press Play, press 1 and click: a beam of light cuts through the dark. Then Check.
Try it yourself
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Lighting = game:GetService("Lighting")
local Settings = require(game.ReplicatedStorage.Settings)
-- Night falls: no sun, a little light everywhere, and fog
Lighting.ClockTime = Settings.ClockTime
Lighting.Ambient = Settings.Ambient
Lighting.OutdoorAmbient = Settings.Ambient
Lighting.FogColor = Color3.new(0, 0, 0)
Lighting.FogEnd = Settings.FogEnd
print("Lights out")
-- Lesson 2: The Flashlight goes here
All lessons in Roblox Game: Escape the Dark
Practice on your own: Roblox playground