I want to create a flashlight with a battery system, but I cant find any tutorials. i need help with the script.
Try this:
local Flashlight = script.Parent.Parent
local On = false
local Battery = 100
Flashlight.Equipped:Connect(function(LocalPlayer)
local Mouse = LocalPlayer:GetMouse()
Mouse.Button1Down:Connect(function()
if On == false then
if Battery > 0 then
Flashlight.Spotlight.Enabled = true
On = true
repeat
Battery -= 1
wait(1) -- Change if you want
until Battery == 0 or On == false
end
else
On = false
Flashlight.Spotlight.Enabled = false
end)
end)
where do I put this script? also is it a local script
Its a Script and put it inside Flashlight>Handle
wait how do add handles (this is my first time making a tool)
Name a part “Handle” and put it inside a tool instance
it isn’t working. i cant see the spotlight. also how to hook power with GUI
You need to put a spotlight inside handle
I’ve made a GUI How can i hook it up to the power script. The flashlight doenst turn of
It wasnt turning on bc i forgot a piece of code but i already fixed it, and if you want to have a battery gui you will ned to make a value tha contains the flashlight battery
I cant test it right now bcz my time ran out
): I have to test it tommorow
You can achieve this pretty easily with something called OOP (Object oriented programming)
local function Flashlight(battery)
battery = battery or "0"
return {
battery = battery,
drain = function(self)
self.battery = self.battery - 1
return self.battery
end
}
end
local Flashlight = Flashlight(100)
for i = 1,100, 1 do
wait(1)
print(Flashlight:drain())
end
This should be what you are hoping for you may also add more elements to your flashlight under the “Flashlight” function!
Where do I put this script? In My GUI?
Can U edit the code? If U already have, than this script doesn’t work.
Try this:
Create a NumberValue inside Handle and set it value to 100
Put your Gui inside Handle
local Flashlight = script.Parent.Parent
local Gui = script.Parent.FlashlightGui
local On = false
local Battery = script.Parent.Battery
Flashlight.Equipped:Connect(function(LocalPlayer)
local Mouse = LocalPlayer:GetMouse()
Mouse.Button1Down:Connect(function()
if On == false then
local FlashlightGui = Gui:Clone()
Gui.Parent = script.Parent.Parent.Parent.Parent.PlayerGui
if Battery.Value > 0 then
Flashlight.Spotlight.Enabled = true
On = true
repeat
Battery.Value -= 1
wait(1) -- Change if you want
until Battery.Value == 0 or On == false
end
else
On = false
Flashlight.Handle.Spotlight.Enabled = false
end)
end)