What do you want to achieve?
This code is the skeleton of a flashlight script that I am trying to make. I am fairly new to coding so I am not sure how to resolve this.
This is supposed to drain “battery” when the flashlight is “on” , and passively regain it when it’s off
What is the issue?
These loops aren’t stopping. It keeps draining the battery and I’m not sure how to fix this
What solutions have you tried so far?
I’ve tried fiddling around with other kinds of loops but they all give me the same results and I am probably too inexperienced to find some kind of work around
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local lightRS = RS:WaitForChild("Light")
local localPlayer = Players.LocalPlayer
local battery = 100
local lightOn = false
local mouse = localPlayer:GetMouse()
local function emitLight(input, gameProcess)
if input.UserInputType == Enum.UserInputType.MouseButton2 and not lightOn then
lightOn = true
print("Light is on")
repeat
print(battery)
battery = battery - 1
wait(.5)
until lightOn == false or battery == 1
end
end
local function closeLight(input, gameProcess)
if input.UserInputType == Enum.UserInputType.MouseButton2 and lightOn then
lightOn = false
print("Light is off")
repeat
print(battery)
battery = battery + 1
wait(.5)
until lightOn == true or battery == 100
end
end
UIS.InputBegan:Connect(emitLight)
UIS.InputBegan:Connect(closeLight)
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local lightRS = RS:WaitForChild("Light")
local localPlayer = Players.LocalPlayer
local battery = 100
local lightOn = false
local mouse = localPlayer:GetMouse()
local function emitLight(input, gameProcess)
if input.UserInputType == Enum.UserInputType.MouseButton2 then
if not lightOn then
lightOn = true
print("Light is on")
while lightOn == true and battery >= 1 do
print(battery)
battery = battery - 1
wait(.5)
end
else
lightOn = false
print("Light is off")
while lightOn == false and battery < 100 do
print(battery)
battery = battery + 1
wait(.5)
end
end
end
end
UIS.InputBegan:Connect(emitLight)
Here’s an updated version if you’d like to use this.
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local lightRS = RS:WaitForChild("Light")
local localPlayer = Players.LocalPlayer
local battery = 100
local lightOn = false
local lastToggle = tick()
local mouse = localPlayer:GetMouse()
local function updateBattery(status, charge)
lastToggle = tick()
local currentToggle = lastToggle
-- If updateBattery is called again last toggle will be changed and this while loop will break
while (currentToggle == lastToggle) do
battery = battery + charge
if battery >= 1 then
break
elseif battery <= 0 then
charge = 1
print("Light is off")
-- Turn light off
end
wait(.5)
end
end
local function toggleLight(input, gameProcess)
if gameProcess then
return
end
if input.UserInputType == Enum.UserInputType.MouseButton2 then
lightOn = not lightOn
if lightOn then
print("Light is on")
updateBattery(lightOn, -1)
else
print("Light is off")
updateBattery(lightOn, 1)
end
end
end
UIS.InputBegan:Connect(toggleLight)