:GetMarkerReachedSignal() firing way too early

I’m trying to make an NPC pick up an axe. Right now I have it so there is two axes, one is invisible and connected to the left hand, and the other is visible and on a table connected to the humanoid root part. What I want it to do is when the hand is on the axe, it will swap which one is transparent and which one isn’t to make it look like it’s being picked up. My problem is that the axe is being ‘picked up’ and ‘placed down’ before the hand is even near the axe on the table. I’ve searched this up and all I could find was that local scripts would be more reliable, but the animation won’t even play in a local script for whatever reason and I would rather it be on the server if possible anyways. (Also if it helps, the animation is 120 fps but slowed down to 0.01 so it could last longer)

The animation:

The code:

local animation = script:WaitForChild('Animation')
local humanoid = script.Parent:WaitForChild('Humanoid')
local move = humanoid:LoadAnimation(animation)
local markerReached = move:GetMarkerReachedSignal("Event")
local markerReached2 = move:GetMarkerReachedSignal("Start")

print(move.TimePosition)
move:Play(0, 1, 0.01)

markerReached:Connect(function()
	print("Move 1:", move.TimePosition)
	script.Parent.Axe.Union.Transparency = 1
	script.Parent.Axe.Part.Transparency = 1
	script.Parent.Axe.Part2.Transparency = 1

	script.Parent.Axe2.Union2.Transparency = 0
	script.Parent.Axe2.Part3.Transparency = 0
	script.Parent.Axe2.Part4.Transparency = 0

end)

markerReached2:Connect(function()
	print("Move 2:", move.TimePosition)
	script.Parent.Axe.Union.Transparency = 0
	script.Parent.Axe.Part.Transparency = 0
	script.Parent.Axe.Part2.Transparency = 0

	script.Parent.Axe2.Union2.Transparency = 1
	script.Parent.Axe2.Part3.Transparency = 1
	script.Parent.Axe2.Part4.Transparency = 1

end)
1 Like

I’m a little confused as to which axe is which, but have you tried just reversing the transparencies for all of them? If it does the opposite of what you’re expecting, then switch your values. Unless this is just a timing issue, in which case yes, local scripts would be more reliable.

Sorry, should have clarified. Axe 2 is connected to the left arm, axe 1 is connected to the HumanoidRootPart. I do think it’s a timing issue, but if I have to use a local script, is there some code that I have to change since I tried it already without changing the code and the animation just doesn’t play?

1 Like

If you’re using a LocalScript, you would have to use the Animator on the Humanoid of your NPC

local animation = script:WaitForChild('Animation')
local humanoid = script.Parent:FindFirstChildOfClass('Humanoid')
local animator = humanoid:FindFirstChildOfClass('Animator')

animator:LoadAnimation(animation)
animation:Play()

I would just like to point out, it seems how you put it, your transparency values are inversed.

markerReached:Connect(function()
	print("Move 1:", move.TimePosition) -- NPC moving animation starts i assume --
	script.Parent.Axe.Union.Transparency = 1 -- Axe on Table disappears --
	script.Parent.Axe.Part.Transparency = 1
	script.Parent.Axe.Part2.Transparency = 1

	script.Parent.Axe2.Union2.Transparency = 0 -- Axe in his hand appears --
	script.Parent.Axe2.Part3.Transparency = 0
	script.Parent.Axe2.Part4.Transparency = 0

end)

Move 2 is actually the start, move 1 is when it’s picked up. I’ll try the other solution right now if I can figure it out :+1:

1 Like

I would have to have a visualization of where the markers are to really know if that is the problem, if you still want it server sided, you might be able to fix that.

I tried changing it to a local script, so here’s this (if there is a solution for server side I can revert back), but anyways, as for a local script it still isn’t animated so I think I did something wrong, just not sure what.

local animation = script:WaitForChild('Animation')
local humanoid = script.Parent:FindFirstChildOfClass('Humanoid')
local animator = humanoid:FindFirstChildOfClass('Animator')

local markerReached = animation:GetMarkerReachedSignal("Event")
local markerReached2 = animation:GetMarkerReachedSignal("Start")

print(animation.TimePosition)
animator:LoadAnimation(animation)
animation:Play(0, 1, 0.01)

markerReached:Connect(function()
	print("Move 1:", animation.TimePosition)
	script.Parent.Axe.Union.Transparency = 1
	script.Parent.Axe.Part.Transparency = 1
	script.Parent.Axe.Part2.Transparency = 1

	script.Parent.Axe2.Union2.Transparency = 0
	script.Parent.Axe2.Part3.Transparency = 0
	script.Parent.Axe2.Part4.Transparency = 0

end)

markerReached2:Connect(function()
	print("Move 2:", animation.TimePosition)
	script.Parent.Axe.Union.Transparency = 0
	script.Parent.Axe.Part.Transparency = 0
	script.Parent.Axe.Part2.Transparency = 0

	script.Parent.Axe2.Union2.Transparency = 1
	script.Parent.Axe2.Part3.Transparency = 1
	script.Parent.Axe2.Part4.Transparency = 1

end)

Not exactly sure what you mean for a visualization of the markers- but here’s this if it helps or if it’s what you mean

Sorry that’s my bad, you have to play the animator instead of the animation, the animator is the animationTrack.

I changed it to this but it still won’t play

local animation = script:WaitForChild('Animation')
local humanoid = script.Parent:FindFirstChildOfClass('Humanoid')
local animator = humanoid:FindFirstChildOfClass('Animator')

local markerReached = animator:GetMarkerReachedSignal("Event")
local markerReached2 = animator:GetMarkerReachedSignal("Start")

print(animator.TimePosition)
animator:LoadAnimation(animation)
animator:Play(0, 1, 0.01)

markerReached:Connect(function()
	print("Move 1:", animator.TimePosition)
	script.Parent.Axe.Union.Transparency = 1
	script.Parent.Axe.Part.Transparency = 1
	script.Parent.Axe.Part2.Transparency = 1

	script.Parent.Axe2.Union2.Transparency = 0
	script.Parent.Axe2.Part3.Transparency = 0
	script.Parent.Axe2.Part4.Transparency = 0

end)

markerReached2:Connect(function()
	print("Move 2:", animator.TimePosition)
	script.Parent.Axe.Union.Transparency = 0
	script.Parent.Axe.Part.Transparency = 0
	script.Parent.Axe.Part2.Transparency = 0

	script.Parent.Axe2.Union2.Transparency = 1
	script.Parent.Axe2.Part3.Transparency = 1
	script.Parent.Axe2.Part4.Transparency = 1

end)

Animating a model locally

local function playAnimationFromServer(character, animation)
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		-- need to use animation object for server access
		local animator = humanoid:FindFirstChildOfClass("Animator")
		if animator then
			local animationTrack = animator:LoadAnimation(animation)
			animationTrack:Play()
			return animationTrack
		end
	end
end

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"

local character = script.Parent

playAnimationFromServer(character, animation)

Thank you for the help but this might take me a little while to understand

1 Like

I did the same and it seems to work fine, you might need to create an Animator inside of your NPC.

Okay so I could be wrong since I’m not the smartest when it comes to programming anything- but I think the script may not be running at all when it’s local script for some reason? I don’t see any of the prints inside of the client log thing- but also don’t see any errors related to it either.

Here’s what I currently have for my code

local animation = script:WaitForChild('Animation')
local humanoid = script.Parent:FindFirstChildOfClass('Humanoid')
local animator = humanoid:FindFirstChildOfClass('Animator')

local markerReached = animator:GetMarkerReachedSignal("Event")
local markerReached2 = animator:GetMarkerReachedSignal("Start")


move = animator:LoadAnimation(animation)
move:Play(0, 1, 0.01)
print(move.TimePosition)

markerReached:Connect(function()
	print("Move 1:", move.TimePosition)
	script.Parent.Axe.Union.Transparency = 1
	script.Parent.Axe.Part.Transparency = 1
	script.Parent.Axe.Part2.Transparency = 1

	script.Parent.Axe2.Union2.Transparency = 0
	script.Parent.Axe2.Part3.Transparency = 0
	script.Parent.Axe2.Part4.Transparency = 0

end)

markerReached2:Connect(function()
	print("Move 2:", move.TimePosition)
	script.Parent.Axe.Union.Transparency = 0
	script.Parent.Axe.Part.Transparency = 0
	script.Parent.Axe.Part2.Transparency = 0

	script.Parent.Axe2.Union2.Transparency = 1
	script.Parent.Axe2.Part3.Transparency = 1
	script.Parent.Axe2.Part4.Transparency = 1

end)

and then here’s what the explorer looks like in case this helps (LocalScript is the code above):
image

Have you tried seeing if it works outside of studio? I’ve had similar problems.

Do you mean fully in game? Or in another studio instance on a separate game?

I’m talking about fully in game. the Published version.

Sorry, had to set it to private and publish it, but yeah, it still doesn’t work.

Maybe use move.KeyframeReached? It runs like this:

move.KeyframeReached:Connect(function(KeyFrame)
if KeyFrame == "InsertAnimationEventName" then -- has to be a string btw
-- ur code
end)
1 Like

Already tried this before with the exact same results- but also the animation still doesn’t play on a local script (and doesn’t sync on a normal script)