Unable to cast value to object, when loading an animation

I tried to make a script so that when the player equips the tool it would play an animation. But things didn’t go so well. When I equip the tool, I keep getting the error “Unable cast value to object”.

Captura

These are the scripts:
Local Script:

local t = script.Parent

local to = game:GetService("ReplicatedStorage").Player.Animations
function send(pl, ty, anim)
	if ty == "e" and typeof(ty) == "string" then
		to:InvokeServer(pl, ty, anim)
	end
end
t.Equipped:Connect(function()
	delay(.1, function()
		local c = t.Parent
		
		local anim = t.Anim.Idle
		send(c, "e", anim)
	end)
end)

Script:

local to = game:GetService("ReplicatedStorage").Player.Animations
to.OnServerInvoke = function(pl, ty, anim)
	local c = pl.Character

	if c then
		local h = c:FindFirstChild("Humanoid")
		if h then
			local tr = h:LoadAnimation(anim)
			tr:Play()
		else
			warn("Humanoid not found in player's character")
		end
	else
		warn("Player character not found")
	end
end

I tried to fix it but it still didn’t work. I checked the inspector and it says that the value of “anim” is equal to “e” and that it is a string.

lulz

Animations are replicated to all users so you can do it on the client.

But the reason your script is failing is that the first argument passed to the .OnServerInvoke callback is always the player who invoked the remote function, then subsequently any arguments passed from the client.

should be (I believe)

to.OnServerInvoke = function(pl, pl2, ty, anim)

or you can just remove the pl from to:InvokeServer as it isn’t useful here:

to:InvokeServer(ty, anim)

Thanks, I’ve been looking for a solution all day!

1 Like

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