The problem associates when the Flashlight is Equipped (in use). When Flashlight is Equipped, an GUI will appear. This is showing the Battery percentage of the Flashlight life span. When the Flashlight is in use, the Battery percentage will decrease every 1.5 second(s). This is all through the Server Side.
This is relating to the factor of the Battery percentage decreasing for all Players even when others may not have the Flashlight Equipped. It will still become a bystander and effect other Players, Battery percentage. – This results into the issue.
How to achieve it so that whilst having the Flashlight functions on the Server. Players can see and hear the Flashlight in motion from other Players.
Just the Battery GUI is separate for all Players, so they have their own long-lasting life, and not run by the same System for all the Players.
Server Side: Flashlight
local HandlerRS = game:GetService("ReplicatedStorage")["Handler (RS)"].Items.Flashlight
local Flashlight = script.Parent.Parent
local Light1 = Flashlight.Handle.SpotLight
local Light2 = Flashlight.Handle.Attachment.SpotLight
local Value = Flashlight.Settings.Battery
local Configure = Flashlight.Settings
local Sounds = HandlerRS
local Equipped = false
local IsOn = false
local Event = nil
local function map(n, start, stop, newStart, newStop, withinBounds)
local value = ((n - start) / (stop - start)) * (newStop - newStart) + newStart
if not withinBounds then
return value
end
if newStart < newStop then
return math.max(math.min(value, newStop), newStart)
else
return math.max(math.min(value, newStart), newStop)
end
end
if HandlerRS:FindFirstChild("kFlashlightEvent") == nil then
local kevent = Instance.new("RemoteEvent", HandlerRS)
kevent.Name = "kFlashlightEvent"
Event = kevent
else
Event = HandlerRS:FindFirstChild("kFlashlightEvent")
end
Flashlight.Equipped:Connect(function()
Sounds.FlashlightEquip:Play()
Equipped = true
end)
Flashlight.Unequipped:Connect(function()
Sounds.FlashlightEquip:Play()
Equipped = false
Light1.Enabled = false
Light2.Enabled = false
IsOn = false
end)
local function TurnOn()
Light1.Enabled = true
Light2.Enabled = true
IsOn = true
end
local function TurnOff()
Light1.Enabled = false
Light2.Enabled = false
IsOn = false
end
local function onToggle(playerName)
if tostring(playerName) == Flashlight.Parent.Name then
Sounds.FlashlightClick:Play()
if IsOn then
TurnOff()
else
if Value.Value > 0 then
TurnOn()
end
end
end
end
Event.OnServerEvent:Connect(onToggle)
while Value.Value > 0 do
if Equipped and IsOn then
Value.Value = Value.Value - 1
Flashlight.Handle.SpotLight.Brightness = map(
math.clamp(Value.Value, 10, 40),
10, 40,
Configure.MinimumBrightness.Value, Configure.MaximumBrightness.Value)
if Value.Value == 0 then
TurnOff()
break
end
end
task.wait(Configure.BatteryRate.Value)
end
Client Side: Battery for Flashlight
local Player = game:GetService("Players").LocalPlayer
local HandlerRS = game:GetService("ReplicatedStorage")["Handler (RS)"].Items.Flashlight
local Flashlight = script.Parent.Parent
local Event = HandlerRS:WaitForChild("kFlashlightEvent")
local Mouse = Player:GetMouse()
Flashlight:WaitForChild("BatteryGui").Parent = Player.PlayerGui
local BatteryGui = Player.PlayerGui:WaitForChild("BatteryGui")
Flashlight.Equipped:Connect(function()
BatteryGui.Enabled = true
end)
Flashlight.Unequipped:Connect(function()
BatteryGui.Enabled = false
end)
Mouse.Button1Down:Connect(function()
Event:FireServer(Player.DisplayName)
end)
while Flashlight.Settings.Battery.Value > 0 do
if Flashlight.Parent.Name ~= "Backpack" then
BatteryGui.BatteryFrame.BatteryText.Text = Flashlight.Settings.Battery.Value
if Flashlight.Settings.Battery.Value == 1 then
BatteryGui.BatteryFrame.BatteryText.Text = 0
break
end
end
task.wait(Flashlight.Settings.BatteryRate.Value)
end
