Bug with Consecutive Animations and GetMarkerReachedSignal, I need help

Hi! I am writing code so that if you open a portable toilet, an NPC will play an animation and execute functions with “GetMarkerReachedSignal”. The problem occurs the second time the “else” part is triggered, where the “Shoot” animation is played. It simply plays the animation but does not execute the functions of “GetMarkerReachedSignal”. Does anyone know why this happens?

Code:

local open = false
local canShoot = false
local tweenS = game:GetService("TweenService")
local OpenCFrame = script.Parent:FindFirstChild("DoorOpen"):GetModelCFrame()
script.Parent.DoorOpen:Destroy()
local CloseCFrame = script.Parent:FindFirstChild("Door"):GetModelCFrame()

local hum = script.Parent.Toilet:FindFirstChildOfClass("Humanoid")

local function tweenDoor(toCFrame)
	tweenS:Create(
		script.Parent:FindFirstChild("Door").PrimaryPart,
		TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut),
		{ CFrame = toCFrame }
	):Play()
end

local function playAnimation(humanoid, animation)
	local anim = humanoid:LoadAnimation(animation)
	anim:Play()
	return anim
end

local function openDoor()
	open = true
	script.Parent.HitBoxDoor.ProximityPrompt.Enabled = false
	tweenDoor(OpenCFrame)
	script.Parent.HitBoxDoor.SFX:Play()
end

local function closeDoor()
	open = false
	tweenDoor(CloseCFrame)
	script.Parent.HitBoxDoor.SFX:Play()
	wait(3)
	script.Parent.HitBoxDoor.ProximityPrompt.Enabled = true
end

script.Parent:FindFirstChild("HitBoxDoor").ProximityPrompt.Triggered:Connect(function(plr)
	if not open then
		local char = plr.Character
		local humP = char:FindFirstChildOfClass("Humanoid")
		script.Parent.HitBoxDoor.ProximityPrompt.Enabled = false

		if hum then
			if not canShoot then
				local anim = playAnimation(hum, script.Animation)
				wait(0.3)
				canShoot = true
				script.fart:Play()

				anim:GetMarkerReachedSignal("Susto"):Connect(function()
					script.shout:Play()
				end)

				anim:GetMarkerReachedSignal("ToiletDoor"):Connect(function()
					closeDoor()
				end)

				openDoor()
				anim.Stopped:Wait()
			else
				local anim = playAnimation(hum, script.Shoot)
				script.Parent.Toilet.TrenchGun.Handle.Transparency = 0
				wait(0.3)

				anim:GetMarkerReachedSignal("Shoot"):Connect(function()
					humP.Health = 0
					script.shoot:Play()
					wait(script.shoot.TimeLength)
					script.Parent.Toilet.TrenchGun.Handle.Transparency = 1
					closeDoor()
					canShoot = false
					anim:Stop()
					anim.Stopped:Wait()
				end)

				openDoor()
				anim.Stopped:Wait()
			end
		end
	end
end)
1 Like

Have you tried moving the wait to after the :GetMarkerReachedSignal? Maybe the signal is firing before you call :GetMarkerReachedSignal.

anim:GetMarkerReachedSignal("Shoot"):Connect(function()
	humP.Health = 0
	script.shoot:Play()
	wait(script.shoot.TimeLength)
	script.Parent.Toilet.TrenchGun.Handle.Transparency = 1
	closeDoor()
	canShoot = false
	-- Why are you stopping the animation? Is it Looped?
	anim:Stop()
	-- Why is this line here? It doesn't do anything.
	--anim.Stopped:Wait()
end)

wait(0.3)

openDoor()

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