Making an Advanced Flashlight System!

  • Intro

Welcome to my tutorial! Today we are making an Advanced Flashlight Script!
Don’t worry this is very easy to make! And also easy to understand of course.

I’ve only spent an easy 6 hours on this script so it shouldn’t be that hard.

So well I am currently making this for an SCP game I am currently making.
So might as well make this Flashlight system tutorial.

Details

Screenshots:

Setup

Tutorial

  • Setup
  1. First you must put 2 folders in ServerScriptService

This will be used so we can organize and insert Server Scripts without having to fear that they would get lost.

This is what it should look like for the most part:
image

  1. Make sure to insert 2 scripts in those 2 folders.

These 2 scripts are going to be used so when we fire a Remote Event from a client sided script, it can receive the event and make the script do something.

And make sure to name it whatever you want btw.

This is what it should look like again:
image

  1. Insert 2 folders on ServerStorage.

This is used to insert items and extract those items coming from those 2 folders, these will be very useful later in the future.

This is what it should look like:
image

  1. Insert a LocalScript into StarterPlayer and then into StarterCharacterScripts.

This LocalScript will be our main script for this system so most of the code that we are about to make are going to be located into these scripts.

This is what it should look like:
image

  1. Insert a this Flashlight Tool into the Tools folders located in ServerStorage. And insert a Part into the Items folder again in ServerStorage.

This is what it should look like again:
image

  1. Next is insert 2 Spotlights into the Part located in ServerStorage.

These 2 Spotlights will be the light for our flashlight.

This is what it should look like:
image

  1. Next is we will insert a folder in ReplicatedStorage, and add 3 RemoteEvents into that folder.

And make sure to name them separately.

This is what it should look like:
image

  1. Next we will insert 2 folders under the LocalScript we just added in and add sound effects for the flashlight on and off part. We will be randomizing the sound so you can add how many you’d like.

This is what it should look like:
image

Client Side Script
  • LocalScript
  1. Variables

First add some variables relocating to ReplicatedStorage, and add in the CurrentCamera from workspace so we can collect the Part to the Players Camera.

local rps = game:GetService("ReplicatedStorage")

local cam = workspace.CurrentCamera

Next add variables relocating the Player and get the Players Humanoid. And get the UserInputService, TweenService, and RunService.

local rps = game:GetService("ReplicatedStorage")

local cam = workspace.CurrentCamera

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local UIS = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local rs = game:GetService("RunService")

Next we will make a variable called “flash” and “equipped”

local rps = game:GetService("ReplicatedStorage")

local cam = workspace.CurrentCamera

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local UIS = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local rs = game:GetService("RunService")

local flash = false
local equipped = false
  1. Functions

Well then after that we will make a function for the UserInputService,
and call it what ever you want. And also don’t forget to add an argument to the function called input, since we will be connecting this function to the UserInputService.

Then we will detect if the player is pressing F so we will be adding this to the script.

local inputIn = function(input)
	if input.KeyCode == Enum.KeyCode.F then
		-- Script
	end
end

Next we will be making a toggle so we will add a check if it hasn’t been pressed yet. And also we will be adding if the tool isn’t equipped so:

local inputIn = function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if not flash then
			if equipped then
				flash = true
			end
		else
			if equipped then
				flash = false
			else
				flash = false
			end
		end
	end
end

So next we will add another check if the player pressed another Key, for me it’s the number 1. This key will be used for cloning the tool and making us equip it.

local inputIn = function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if not flash then
			if equipped then
				flash = true
			end
		else
			if equipped then
				flash = false
			else
				flash = false
			end
		end
	end
	if input.KeyCode == Enum.KeyCode.One then
		if not equipped then
			equipped = true
		else
			if flash then
				flash = false
			end
			flash = false
			equipped = false
		end
	end
end

Next we will be adding another function for the flashlight Mechanic!
But at this one we will be detecting if the part has been cloned so!

Oh yeah and I almost forgot, add these to the variables! These are the RemoveEvents and the Clones to the script!

Variables:

local rps = game:GetService("ReplicatedStorage")

local cam = workspace.CurrentCamera

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local tool = workspace:FindFirstChild("Flashlight")

local UIS = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local rs = game:GetService("RunService")

local remoteEvent = rps:WaitForChild("RemoveEvents"):WaitForChild("FlashlightTool")
local lightOn = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOn")
local lightOff = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOff")

local flash = false
local equipped = false

Function:

local flashlight = function()
	if tool then
		if equipped then
			if flash then
			
		end
	end
end

So now this part is optional but we will be adding a Tween so if the Player turns his/her camera, it will tween and go with it. You will put this after the “If tool then” check.

With Tween:

ts:Create(tool, TweenInfo.new(.25, Enum.EasingStyle.Sine), {CFrame = cam.CFrame}):Play()

Without Tween:

tool.CFrame = cam.CFrame

Next we will make the tool invisible! By using a for loop this is very easy to achieve!

local flashlight = function()
	if plr.Backpack:FindFirstChild("Flashlight") or char:FindFirstChild("Flashlight") then
		hum:EquipTool(plr.Backpack:FindFirstChild("Flashlight"))
	end
	if tool then
		ts:Create(tool, TweenInfo.new(.25, Enum.EasingStyle.Sine), {CFrame = cam.CFrame}):Play()
		if equipped then
			if char:FindFirstChild("Flashlight") then
				for i, tool in pairs(char:FindFirstChild("Flashlight"):GetChildren()) do
					if tool:IsA("BasePart") then
						tool.Transparency = 1
					end
				end
			end
		end
	end
end

So now after that pain, we can finally make the light part!

First we would want to detect if the flash variable is turned on. So we make a check for it. And then after you’ve made that we would like to make a for loop again, so we won’t have to copy and paste the light variables so this is what it would look like!

local flashlight = function()
	if tool then
		ts:Create(tool, TweenInfo.new(.25, Enum.EasingStyle.Sine), {CFrame = cam.CFrame}):Play()
		if equipped then
			if char:FindFirstChild("Flashlight") then
				for i, tool in pairs(char:FindFirstChild("Flashlight"):GetChildren()) do
					if tool:IsA("BasePart") then
						tool.Transparency = 1
					end
				end
			end
			if flash then
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = true
					end
				end
			else
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = false
					end
				end
			end
		end
	end
end

Now we would want to check again if the player has unequipped the tool, so we would also put an else function to check if the tool is unequipped!

local flashlight = function()
	if plr.Backpack:FindFirstChild("Flashlight") or char:FindFirstChild("Flashlight") then
		hum:EquipTool(plr.Backpack:FindFirstChild("Flashlight"))
	end
	if tool then
		ts:Create(tool, TweenInfo.new(.25, Enum.EasingStyle.Sine), {CFrame = cam.CFrame}):Play()
		if equipped then
			if char:FindFirstChild("Flashlight") then
				for i, tool in pairs(char:FindFirstChild("Flashlight"):GetChildren()) do
					if tool:IsA("BasePart") then
						tool.Transparency = 1
					end
				end
			end
			if flash then
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = true
					end
				end
			else
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = false
					end
				end
			end
		else
			for i, light in pairs(tool:GetChildren()) do
				if light:IsA("SpotLight") then
					light.Enabled = false
				end
			end
		end
	end
end

Next we will now fire an event from our Local Script so we can transmit the signal to the Clone Script.

local inputIn = function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if not flash then
			if equipped then
				flash = true
			end
		else
			if equipped then
				flash = false
			else
				flash = false
			end
		end
	end
	if input.KeyCode == Enum.KeyCode.One then
		if not equipped then
			remoteEvent:FireServer()
			
			wait()
			
			equipped = true
		else
			if flash then
				flash = false
			end

			flash = false
			equipped = false
		end
	end
end

Next we will force equip the tool to the Player. So when the tool clones, the player will automatically equip it!


local flashlight = function()
	-- Equip
	if plr.Backpack:FindFirstChild("Flashlight") or char:FindFirstChild("Flashlight") then
		hum:EquipTool(plr.Backpack:FindFirstChild("Flashlight"))
	end
	if tool then
		ts:Create(tool, TweenInfo.new(.25, Enum.EasingStyle.Sine), {CFrame = cam.CFrame}):Play()
		if equipped then
			if char:FindFirstChild("Flashlight") then
				for i, tool in pairs(char:FindFirstChild("Flashlight"):GetChildren()) do
					if tool:IsA("BasePart") then
						tool.Transparency = 1
					end
				end
			end
			if flash then
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = true
					end
				end
			else
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = false
					end
				end
			end
		else
			-- Unequip
			if char:FindFirstChild("Flashlight") or plr.Backpack:FindFirstChild("Flashlight") then
				char:FindFirstChild("Flashlight").Parent = nil
			end
			
			for i, light in pairs(tool:GetChildren()) do
				if light:IsA("SpotLight") then
					light.Enabled = false
				end
			end
		end
	end
end

And this is the final part of the Client side script so we will now connect these functions to it’s desired parts!

UIS.InputBegan:Connect(inputIn)

rs.RenderStepped:Connect(flashlight)

Now finally the Flashlight script is finally done! This is what it should look like!

local rps = game:GetService("ReplicatedStorage")

local cam = workspace.CurrentCamera

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local tool = workspace:FindFirstChild("Flashlight")

local UIS = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local rs = game:GetService("RunService")

local remoteEvent = rps:WaitForChild("RemoveEvents"):WaitForChild("FlashlightTool")

local flash = false
local equipped = false

local inputIn = function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if not flash then
			if equipped then
				flash = true
			end
		else
			if equipped then
				flash = false
			else
				flash = false
			end
		end
	end
	if input.KeyCode == Enum.KeyCode.One then
		if not equipped then
			remoteEvent:FireServer()
			
			wait()
			
			equipped = true
		else
			if flash then
				flash = false
			end
			
			flash = false
			equipped = false
		end
	end
end

local flashlight = function()
	if plr.Backpack:FindFirstChild("Flashlight") or char:FindFirstChild("Flashlight") then
		hum:EquipTool(plr.Backpack:FindFirstChild("Flashlight"))
	end
	if tool then
		ts:Create(tool, TweenInfo.new(.25, Enum.EasingStyle.Sine), {CFrame = cam.CFrame}):Play()
		if equipped then
			if char:FindFirstChild("Flashlight") then
				for i, tool in pairs(char:FindFirstChild("Flashlight"):GetChildren()) do
					if tool:IsA("BasePart") then
						tool.Transparency = 1
					end
				end
			end
			if flash then
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = true
					end
				end
			else
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = false
					end
				end
			end
		else
			if char:FindFirstChild("Flashlight") or plr.Backpack:FindFirstChild("Flashlight") then
				char:FindFirstChild("Flashlight").Parent = nil
			end
			
			for i, light in pairs(tool:GetChildren()) do
				if light:IsA("SpotLight") then
					light.Enabled = false
				end
			end
		end
	end
end

UIS.InputBegan:Connect(x1)

rs.RenderStepped:Connect(flashlight)
Cloning and Server Side Scripts
  1. Clones

Okay so first you would want to go to ServerStorage, and go to the Script inside the Clones folder.

And then inside that Script, We will be adding some variables on where those things are located.

local rps = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")

local flashlight = ss:WaitForChild("Items"):WaitForChild("Flashlight")
local flashlightTool = ss:WaitForChild("Tools"):WaitForChild("Flashlight")

local remoteEvent = rps:WaitForChild("RemoveEvents"):WaitForChild("FlashlightTool")

So next we will be creating a clone function, and a remove function when the Player leaves the game.

local clone = function()
	local clone = flashlight:Clone()
	clone.Parent = workspace
end

local destroy = function()
	local clone = workspace:FindFirstChild("Flashlight")
	clone:Destroy()
end

Now we will be adding the tool clone function. So we will clone the tool to the Player’s backpack so the Client Script can equip the tool.

local cloneTool = function(plr)
	local tool = flashlightTool:Clone()
	tool.Parent = game.Players[plr.Name].Backpack
end

Now time to connect those functions!

game.Players.PlayerAdded:Connect(function(plr) -- When player joins the game
	plr.CharacterAdded:Connect(function() -- When Player Character is available
		clone() -- Cloning the part
	end)
end)

game.Players.PlayerRemoving:Connect(destroy) -- Makes the parts parent to nil

remoteEvent.OnServerEvent:Connect(cloneTool) -- Clones the tool when event is fired

Full Script:

local rps = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")

local flashlight = ss:WaitForChild("Items"):WaitForChild("Flashlight")
local flashlightTool = ss:WaitForChild("Tools"):WaitForChild("Flashlight")

local remoteEvent = rps:WaitForChild("RemoveEvents"):WaitForChild("FlashlightTool")

local clone = function()
	local clone = flashlight:Clone()
	clone.Parent = workspace
end

local destroy = function()
	local clone = workspace:FindFirstChild("Flashlight")
	clone:Destroy()
end

local cloneTool = function(plr)
	local tool = flashlightTool:Clone()
	tool.Parent = game.Players[plr.Name].Backpack
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		clone()
	end)
end)

game.Players.PlayerRemoving:Connect(destroy)

remoteEvent.OnServerEvent:Connect(cloneTool)
  1. Server Visuals

Okay so this part will just be the light part, so the other person can see the players flashlight turning on, and off.

Adding and on, and off Server Events.

So first add more variables to the Flashlight script from StarterCharacterScripts.

local lightOn = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOn")
local lightOff = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOff")

Then after that we will be adding a Fire Event on the flashlight function.

local flashlight = function()
	if plr.Backpack:FindFirstChild("Flashlight") or char:FindFirstChild("Flashlight") then
		hum:EquipTool(plr.Backpack:FindFirstChild("Flashlight"))
	end
	if tool then
		ts:Create(tool, TweenInfo.new(.25, Enum.EasingStyle.Sine), {CFrame = cam.CFrame}):Play()
		if equipped then
			if char:FindFirstChild("Flashlight") then
				for i, tool in pairs(char:FindFirstChild("Flashlight"):GetChildren()) do
					if tool:IsA("BasePart") then
						tool.Transparency = 1
					end
				end
			end
			if flash then
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = true
					end
				end
				lightOn:FireServer()
			else
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = false
					end
				end
				lightOff:FireServer()
			end
		else
			if char:FindFirstChild("Flashlight") or plr.Backpack:FindFirstChild("Flashlight") then
				char:FindFirstChild("Flashlight").Parent = nil
			end
			
			for i, light in pairs(tool:GetChildren()) do
				if light:IsA("SpotLight") then
					light.Enabled = false
				end
			end
			lightOff:FireServer()
		end
	end
end

Then after that go into ServerScriptService, then go to the TurnLight Folders and click the Script inside it.

So first we will be making variables again, yay…
Again these variables will just be relocating to those things so, this is what it should look like:

local rps = game:GetService("ReplicatedStorage")

local lightOn = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOn")
local lightOff = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOff")

Now we will be making 2 functions on turning the flashlight on and off. So we will be detecting if the flashlight is available from the character then getting that SurfaceLight and turning it off.

local turnLightOn = function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	if char:FindFirstChild("Flashlight") then
		char:FindFirstChild("Flashlight"):WaitForChild("Front"):WaitForChild("SurfaceLight").Enabled = true
	end
end

local turnLightOff = function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	if char:FindFirstChild("Flashlight") then
		char:FindFirstChild("Flashlight"):WaitForChild("Front"):WaitForChild("SurfaceLight").Enabled = false
	end
end

Then finally we can connect it to the Server Events.

lightOn.OnServerEvent:Connect(turnLightOn)
lightOff.OnServerEvent:Connect(turnLightOff)

Full Script:

local rps = game:GetService("ReplicatedStorage")

local lightOn = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOn")
local lightOff = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOff")

local turnLightOn = function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	if char:FindFirstChild("Flashlight") then
		char:FindFirstChild("Flashlight"):WaitForChild("Front"):WaitForChild("SurfaceLight").Enabled = true
	end
end

local turnLightOff = function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	if char:FindFirstChild("Flashlight") then
		char:FindFirstChild("Flashlight"):WaitForChild("Front"):WaitForChild("SurfaceLight").Enabled = false
	end
end

lightOn.OnServerEvent:Connect(turnLightOn)
lightOff.OnServerEvent:Connect(turnLightOff)

Then that’s all for this part. Please check sound for the sound bit.

Sound Randomizer
  1. Getting the sounds

As I said earlier on the Setup part, add sounds to the folder for this part. So I hope you did that, now we will be making a Sound Randomizer for this!

First go back to your Client Script on StarterCharacterScripts. And go to the inputIn function. Then we will be making a simple randomizer using math.random.

And then add it in after the Flash = true part.

This is what it should look like:

local inputIn = function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if not flash then
			if equipped then
				flash = true
				
				-- On Randomizer
				local OnSounds = script:WaitForChild("On"):GetChildren()
				local randomOn = math.random(1, #OnSounds)
				local randomSoundOn = OnSounds[randomOn]
				-- On Randomizer

				randomSoundOn:Play() -- Play the sound
			end
		else
			if equipped then
				flash = false
				
				-- Off Randomizer
				local OffSounds = script:WaitForChild("Off"):GetChildren()
				local randomOff = math.random(1, #OffSounds)
				local randomSoundOff = OffSounds[randomOff]
				-- Off Randomizer

				randomSoundOff:Play() -- Play the sound
			else
				flash = false
				
				-- Off Randomizer
				local OffSounds = script:WaitForChild("Off"):GetChildren()
				local randomOff = math.random(1, #OffSounds)
				local randomSoundOff = OffSounds[randomOff]
				-- Off Randomizer

				randomSoundOff:Play() -- Play the sound
			end
		end
	end
	if input.KeyCode == Enum.KeyCode.One then
		if not equipped then
			remoteEvent:FireServer()
			
			wait()
			
			equipped = true
		else
			if flash then
				-- Off Randomizer
				local OffSounds = script:WaitForChild("Off"):GetChildren()
				local randomOff = math.random(1, #OffSounds)
				local randomSoundOff = OffSounds[randomOff]
				-- Off Randomizer

				randomSoundOff:Play() -- Play the sound
				
				flash = false
			end
			
			flash = false
			equipped = false
		end
	end
end
Full Script
  • Heres the full script!

Client Script:

local rps = game:GetService("ReplicatedStorage")

local cam = workspace.CurrentCamera

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local tool = workspace:FindFirstChild("Flashlight")

local UIS = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local rs = game:GetService("RunService")

local remoteEvent = rps:WaitForChild("RemoveEvents"):WaitForChild("FlashlightTool")
local lightOn = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOn")
local lightOff = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOff")

local flash = false
local equipped = false

local inputIn = function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if not flash then
			if equipped then
				flash = true
				
				local OnSounds = script:WaitForChild("On"):GetChildren()
				local randomOn = math.random(1, #OnSounds)
				local randomSoundOn = OnSounds[randomOn]

				randomSoundOn:Play()
			end
		else
			if equipped then
				flash = false
				
				local OffSounds = script:WaitForChild("Off"):GetChildren()
				local randomOff = math.random(1, #OffSounds)
				local randomSoundOff = OffSounds[randomOff]

				randomSoundOff:Play()
			else
				flash = false
				
				local OffSounds = script:WaitForChild("Off"):GetChildren()
				local randomOff = math.random(1, #OffSounds)
				local randomSoundOff = OffSounds[randomOff]

				randomSoundOff:Play()
			end
		end
	end
	if input.KeyCode == Enum.KeyCode.One then
		if not equipped then
			remoteEvent:FireServer()
			
			wait()
			
			equipped = true
		else
			if flash then
				local OffSounds = script:WaitForChild("Off"):GetChildren()
				local randomOff = math.random(1, #OffSounds)
				local randomSoundOff = OffSounds[randomOff]

				randomSoundOff:Play()
				
				flash = false
			end
			
			flash = false
			equipped = false
		end
	end
end

local flashlight = function()
	if plr.Backpack:FindFirstChild("Flashlight") or char:FindFirstChild("Flashlight") then
		hum:EquipTool(plr.Backpack:FindFirstChild("Flashlight"))
	end
	if tool then
		ts:Create(tool, TweenInfo.new(.25, Enum.EasingStyle.Sine), {CFrame = cam.CFrame}):Play()
		if equipped then
			if char:FindFirstChild("Flashlight") then
				for i, tool in pairs(char:FindFirstChild("Flashlight"):GetChildren()) do
					if tool:IsA("BasePart") then
						tool.Transparency = 1
					end
				end
			end
			if flash then
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = true
					end
				end
				
				lightOn:FireServer()
			else
				for i, light in pairs(tool:GetChildren()) do
					if light:IsA("SpotLight") then
						light.Enabled = false
					end
				end
				
				lightOff:FireServer()
			end
		else
			if char:FindFirstChild("Flashlight") or plr.Backpack:FindFirstChild("Flashlight") then
				char:FindFirstChild("Flashlight").Parent = nil
			end
			
			for i, light in pairs(tool:GetChildren()) do
				if light:IsA("SpotLight") then
					light.Enabled = false
				end
			end
			
			lightOff:FireServer()
		end
	end
end

UIS.InputBegan:Connect(inputIn)

rs.RenderStepped:Connect(flashlight)

Clone Script:

local rps = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")

local flashlight = ss:WaitForChild("Items"):WaitForChild("Flashlight")
local flashlightTool = ss:WaitForChild("Tools"):WaitForChild("Flashlight")

local remoteEvent = rps:WaitForChild("RemoveEvents"):WaitForChild("FlashlightTool")

local clone = function()
	local clone = flashlight:Clone()
	clone.Parent = workspace
end

local destroy = function()
	local clone = workspace:FindFirstChild("Flashlight")
	clone:Destroy()
end

local cloneTool = function(plr)
	local tool = flashlightTool:Clone()
	tool.Parent = game.Players[plr.Name].Backpack
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		clone()
	end)
end)

game.Players.PlayerRemoving:Connect(destroy)

remoteEvent.OnServerEvent:Connect(cloneTool)

Turn light on/off Script:

local rps = game:GetService("ReplicatedStorage")

local lightOn = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOn")
local lightOff = rps:WaitForChild("RemoveEvents"):WaitForChild("lightOff")

local turnLightOn = function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	if char:FindFirstChild("Flashlight") then
		char:FindFirstChild("Flashlight"):WaitForChild("Front"):WaitForChild("SurfaceLight").Enabled = true
	end
end

local turnLightOff = function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	if char:FindFirstChild("Flashlight") then
		char:FindFirstChild("Flashlight"):WaitForChild("Front"):WaitForChild("SurfaceLight").Enabled = false
	end
end

lightOn.OnServerEvent:Connect(turnLightOn)
lightOff.OnServerEvent:Connect(turnLightOff)
  • Finale

And then that’s basically all! I hope you all understand this tutorial, I took a long time learning and figuring this script out my self, as I’m currently just a newbie cause I started scripting like a month ago.

Well I hope the tutorial was simple like I hope it was. So thanks for listening!

I also have a model for this. If you want this, the model is here:

Click here!

If you all have any bugs, please reply to this tutorial.

29 Likes

This is dope! It’s very beginner friendly too. It’s easy to understand and you explain it well.

2 Likes

Thank you so much for the reply!

You’re welcome! It’s pretty sad that there’s so little amount of people seeing this.

1 Like

Is it just me or is it that when the flashlight is on in the third picture, it looks exactly the same as the first 2?

1 Like

I forgot to point it at the ground sorry.

1 Like

Oh wait sorry actually. I made the flashlight invisible for first person horror games.
If you want to remove that feature, go to the flashlight function and remove the,

for i, tool in pairs(char:FindFirstChild("Flashlight"):GetChildren()) do
	if tool:IsA("BasePart") then
		tool.Transparency = 1
	end
end

part after the if equipped part.

1 Like

As a bit of performance advice, I suggest using a lerp function instead of tweening the flashlight to the Camera’s CFrame, since creating a tween every frame can greatly decrease performance.

If you haven’t heard of lerp (linear interpolation) before, it essentially returns a value from a to b based on t, which is a number from 0-1. t is essentially the progress from a to b. If t is 0.5, the returned value is half way between a and b.

To use lerp for smoothening, you can use it in your RenderStepped function. First of all, add a parameter to your RenderStepped function called “deltaTime” or “dt”. RenderStepped returns one parameter, called Delta Time, which is the time in seconds it took to reach the next frame.

Then, replace the tweening line inside the function with:
tool.CFrame = tool.CFrame:Lerp(camera.CFrame,deltaTime*factor)

  • factor is essentially the speed of the flashlight moving. The higher the number, the faster it gets to the final point. (I recommend 6-8)

After this, you’ve essentially added a smooth moving flashlight without using tweens.

4 Likes

Good idea. [ Something to fill this part in ]

after deleting after equipped part function on flashlight function it just doesnt work
no flashlight or anything

I said after the equipped part, and not the equipped part itself.

Maybe the tool isn’t present at the workspace?