I’m making a horror game the sort of requires you to have a flashlight.
The flashlight works really well, and in fact better than most horror game flashlights. I haven’t found any issues with the way I made it, yet.
Well. Except for one.
The script runs in StarterCharacterScripts, so only the player can see it.
I want EVERY player to be able to see the flashlight if they want too… But I have no idea on how I need to do that. Every single person I’ve seen talk about this always recommend adding a Remote Event in ReplicatedStorage and a script in ServerScriptService calling for that Event.
But none of them ever explain on what to do once you’ve done that.
Can anyone explain or help me with this issue?
Here’s the script:
local Head = game.Players.LocalPlayer.Character:WaitForChild("Head")
local Flashlight = Instance.new("SpotLight")
local Mouse = game.Players.LocalPlayer:GetMouse()
local LightSound = Instance.new("Sound")
LightSound.SoundId = "rbxassetid://5991592592" -- Roblox ID, of the Sound that plays when the Flashlight turns on or off.
LightSound.Parent = Head -- What the Sound is parented to.
Flashlight.Parent = Head -- What The Flashlight Sound is parented to.
Flashlight.Shadows = true -- If the objects infront of the Light, cast shadows or not.
Flashlight.Face = "Front" -- Direction of the Light.
Flashlight.Angle = 45 -- How far the Light spreads.
Flashlight.Brightness = 4 -- Brightness of the Light.
Flashlight.Range = 30 -- How far the Light goes.
Flashlight.Enabled = false
if game.Lighting.ExposureCompensation == 7 then
Flashlight.Enabled = false
else
Flashlight.Enabled = true
end
function LightCalled()
if Flashlight.Enabled == false then
LightSound.PlaybackSpeed = 1.2
LightSound.Volume = 0.25
task.wait(0.05)
LightSound:Play()
Flashlight.Face = "Front" -- Direction of the Light.
Flashlight.Angle = 45 -- How far the Light spreads.
Flashlight.Brightness = 4 -- Brightness of the Light.
Flashlight.Range = 30 -- How far the Light goes.
Flashlight.Enabled = true
elseif Flashlight.Enabled == true then
LightSound.PlaybackSpeed = 1
LightSound.Volume = 0.15
task.wait(0.05)
LightSound:Play()
Flashlight.Face = "Front" -- Direction of the Light.
Flashlight.Angle = 0 -- How far the Light spreads.
Flashlight.Brightness = 0 -- Brightness of the Light.
Flashlight.Range = 0 -- How far the Light goes.
Flashlight.Enabled = false
end
end
Mouse.KeyDown:Connect(function(key)
key = key:lower()
if key == "f" then -- Flashlight Keybind
LightCalled()
end
end)
-- LightOff = PS = 1
-- LightOn = PS = 1.2
-- LightOff = Vo = 0.15
-- LightOn = Vo = 0.25
Ignore The “If Exposure Compensation == 7” line. I completely forgot to take that out
** I’m a very early scripter so I was experimenting**
You could use the PlayerAdded Function in a Server Script in ServerScriptService to get the local player and to show it in the server. Because LocalScripts only run on the client, so the flashlight is only visible for the client who presses it.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local Head = player.Character:WaitForChild("Head")
local Flashlight = Instance.new("SpotLight")
local Mouse = player:GetMouse()
local LightSound = Instance.new("Sound")
LightSound.SoundId = "rbxassetid://5991592592" -- Roblox ID, of the Sound that plays when the Flashlight turns on or off.
LightSound.Parent = Head -- What the Sound is parented to.
Flashlight.Parent = Head -- What The Flashlight Sound is parented to.
Flashlight.Shadows = true -- If the objects infront of the Light, cast shadows or not.
Flashlight.Face = "Front" -- Direction of the Light.
Flashlight.Angle = 45 -- How far the Light spreads.
Flashlight.Brightness = 4 -- Brightness of the Light.
Flashlight.Range = 30 -- How far the Light goes.
Flashlight.Enabled = false
if game.Lighting.ExposureCompensation == 7 then
Flashlight.Enabled = false
else
Flashlight.Enabled = true
end
local function LightCalled()
if Flashlight.Enabled == false then
LightSound.PlaybackSpeed = 1.2
LightSound.Volume = 0.25
task.wait(0.05)
LightSound:Play()
Flashlight.Face = "Front" -- Direction of the Light.
Flashlight.Angle = 45 -- How far the Light spreads.
Flashlight.Brightness = 4 -- Brightness of the Light.
Flashlight.Range = 30 -- How far the Light goes.
Flashlight.Enabled = true
elseif Flashlight.Enabled == true then
LightSound.PlaybackSpeed = 1
LightSound.Volume = 0.15
task.wait(0.05)
LightSound:Play()
Flashlight.Face = "Front" -- Direction of the Light.
Flashlight.Angle = 0 -- How far the Light spreads.
Flashlight.Brightness = 0 -- Brightness of the Light.
Flashlight.Range = 0 -- How far the Light goes.
Flashlight.Enabled = false
end
end
Mouse.KeyDown:Connect(function(key)
key = key:lower()
if key == "f" then -- Flashlight Keybind
LightCalled()
end
end)
end)
This script is client-sided. EVERYTHING that happens in a LocalScript or a module required by a LocalScript will not replicate to other players. You will need to recreate the flashlight on the Server in order for it to replicate. You should utilize RemoteEvents in order to achieve your effect.
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
Players.PlayerAdded:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local Head = char:WaitForChild("Head")
local Flashlight = Instance.new("SpotLight")
local Mouse = player:GetMouse()
local LightSound = Instance.new("Sound")
LightSound.SoundId = "rbxassetid://5991592592" -- Roblox ID, of the Sound that plays when the Flashlight turns on or off.
LightSound.Parent = Head -- What the Sound is parented to.
Flashlight.Parent = Head -- What The Flashlight Sound is parented to.
Flashlight.Shadows = true -- If the objects infront of the Light, cast shadows or not.
Flashlight.Face = "Front" -- Direction of the Light.
Flashlight.Angle = 45 -- How far the Light spreads.
Flashlight.Brightness = 4 -- Brightness of the Light.
Flashlight.Range = 30 -- How far the Light goes.
Flashlight.Enabled = false
if game.Lighting.ExposureCompensation == 7 then
Flashlight.Enabled = false
else
Flashlight.Enabled = true
end
local function LightCalled()
if Flashlight.Enabled == false then
LightSound.PlaybackSpeed = 1.2
LightSound.Volume = 0.25
task.wait(0.05)
LightSound:Play()
Flashlight.Face = "Front" -- Direction of the Light.
Flashlight.Angle = 45 -- How far the Light spreads.
Flashlight.Brightness = 4 -- Brightness of the Light.
Flashlight.Range = 30 -- How far the Light goes.
Flashlight.Enabled = true
elseif Flashlight.Enabled == true then
LightSound.PlaybackSpeed = 1
LightSound.Volume = 0.15
task.wait(0.05)
LightSound:Play()
Flashlight.Face = "Front" -- Direction of the Light.
Flashlight.Angle = 0 -- How far the Light spreads.
Flashlight.Brightness = 0 -- Brightness of the Light.
Flashlight.Range = 0 -- How far the Light goes.
Flashlight.Enabled = false
end
end
-- when mouse keydown the lightcalled
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
LightCalled()
end
end)
end)
NOTE: Mouse.KeyDown is Deprecated, it won’t really work anymore.
I used Roblox new AI Assistant to help me with this one. It actually worked surprisingly. Now I am just going to straighten the script up a bit, and customize to my hearts content.
The Issue is that a Server Script cannot detect if the Player has pressed a specific key. That is a feature specifically for Local Scripts ONLY.
To fix this, you must add a Remote Function or Remote Event, Which is what I used and communicate through Local Scripts and Server Scripts to fire the event and let the server script know that it must Toggle the Flashlight.
I used the Roblox Assistant AI to help me fix this issue, and have gotten the script to work.