Help with a flashlight system!

You can write your topic however you want, but you need to answer these questions:

  1. 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)

  2. 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:

  3. 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)
3 Likes

By the way, yes i am aware that my character is not playing any animations. That’s fine for now since it was a temporary animation player system i was making for the flashlight and will soon use animation replication since this is a multiplayer game.

That’s because you are accumulating multiple same connection each time the function is called. Just change :Connect to :Once

Animations["Toggle"]:GetMarkerReachedSignal("Toggle"):Once(function()
	LightItemModule:ToggleLightItem(Flashlight, true)
	Sounds.TurnOn:Play()
	print("played on")
end)
Animations["Toggle"]:GetMarkerReachedSignal("Toggle"):Once(function()
	LightItemModule:ToggleLightItem(Flashlight, false)
	Sounds.TurnOff:Play()
	print("played off")
end)
2 Likes

I… didn’t even know that was a thing… dang, well thanks! I don’t know why luau works this way but whatever gets the job done.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.