You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I would like to have a flashlight tool that can be toggled on and off. (With player animations) -
What is the issue? Include screenshots / videos if possible!
The current issue is, i’ve added prints in places of my script and i’ve found out that, once you turn it on, it works fine, but when you turn it off it turns it on again, and it keeps multiplying the amount of times it turns on and off each toggle.
Here’s the output:
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried searching around the developer forums, nobody had the same issue. adding debounce/yield to the script did nothing and i’m currently at a loss.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
Here’s the current toggle function i have for the flashlight:
local function ToggleFlashlight()
if not Yield then
Yield = true
if Flashlight:GetAttribute("Enabled") == false then
print(Flashlight:GetAttribute("Enabled"))
local WasStalling = false
for _, Animation in Flashlight.Parent.Humanoid:GetPlayingAnimationTracks() do
if Animation.Name == "IdleOff" then
WasStalling = true
end
end
for _, Animation in Animations do
Animation:Stop()
end
if WasStalling then
Animations["Equip"]:Play()
task.wait(Animations["Equip"].Length)
end
Animations["Toggle"]:Play()
Animations["Toggle"]:GetMarkerReachedSignal("Toggle"):Connect(function()
LightItemModule:ToggleLightItem(Flashlight, true)
Sounds.TurnOn:Play()
print("played on")
end)
task.wait(Animations["Toggle"].Length)
Animations["IdleOn"]:Play()
Flashlight:SetAttribute("Enabled", true)
task.wait(2)
Yield = false
elseif Flashlight:GetAttribute("Enabled") == true then
print(Flashlight:GetAttribute("Enabled"))
for _, Animation in Animations do
Animation:Stop()
end
Animations["Toggle"]:Play()
Animations["Toggle"]:GetMarkerReachedSignal("Toggle"):Connect(function()
LightItemModule:ToggleLightItem(Flashlight, false)
Sounds.TurnOff:Play()
print("played off")
end)
task.wait(Animations["Toggle"].Length)
Animations["IdleOn"]:Play()
Flashlight:SetAttribute("Enabled", false)
task.wait(2)
Yield = false
task.wait(1)
if Flashlight:GetAttribute("Enabled") == false then
Animations["IdleOn"]:Stop()
Animations["UnEquip"]:Play()
task.wait(Animations["UnEquip"].Length)
Animations["IdleOff"]:Play()
end
end
end
end
Flashlight.Activated:Connect(ToggleFlashlight)