Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Unable to play animation in script + how to make remote event only fire when tool equipped?

It says that on line 9, it is unable to cast a value to the object, and i have no idea what to do about it, and also the remote event i put in immediately fires when you join the game, and i have no idea how to code it since I'm a horrible coder. Please help.

local rep = game:GetService("ReplicatedStorage")
local PunchEquipped = rep.PunchEquipped
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local tool = script.Parent
local Animation = "https://web.roblox.com/library/10336312893/Fist-Animation"
local PlayAnim = Animator:LoadAnimation("Animation")
        if tool.Equipped then
	PunchEquipped.OnServerEvent(Animation)
	if Player == tool.Parent then
		print('Tool is equipped')
		Animation.AnimationPriority = Enum.AnimationPriority.Action
		Animation.AnimationID = "https://web.roblox.com/library/10336312893/Fist-Animation"
		Animation.Parent = Character
		PlayAnim:Play()
	end
end
 

Unable to cast value to Object - Client - LocalScript:9

1 answer

In line 10 you’re trying to check if Tool.Equipped returns true, but it’s an event, not a boolean. You have to type Tool.Equipped:Connect(function() which will run if the tool has been equipped. I also recommend making the AnimationTrack before Tool.Equipped. And use “rbxassetid://youranimationid” instead of the link of your animation from the website. So it will be from “https://web.roblox.com/library/10336312893/Fist-Animation” to “rbxassetid://10336312893“.

And wait, before I forget, run the animation in a server script so other players can see your animation. So fire the PunchEquipped to the server instead.

--Client (Local Script inside the tool)
local rep = game:GetService("ReplicatedStorage")
local PunchEquipped = rep.PunchEquipped
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local tool = script.Parent
local AnimationId = "rbxassetid://10336312893"
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId

tool.Equipped:Connect(function()
	PunchEquipped:FireServer(Animator, Animation, "equip")
end)

tool.Unequipped:Connect(function()
	PunchEquipped:FireServer(Animator, Animation, "unequip")
end)

--Server (Normal Script inside the tool or ServerScriptService)
local rep = game:GetService("ReplicatedStorage")
local PunchEquipped = rep.PunchEquipped

PunchEquipped.OnServerEvent:Connect(function(player: Player, animator: Animator, animation: Animation, toolStatus: string)
	for _, animTrack in pairs(animator:GetPlayingAnimationTracks()) do
		if animTrack.Animation.AnimationId ~= animation.AnimationId then
			if toolStatus:lower() == "equip" then
				local PlayAnim = animator:LoadAnimation(animation)
				
				repeat task.wait() until PlayAnim.Length > 0
				
				PlayAnim:Play()
			end
		else
			if toolStatus:lower() == "unequip" then
				animTrack:Stop()
			end
		end
	end
end)

Note: This script is untested and just a possible solution. If there are any errors or mistakes, lemme know. ;)

4 comments

0

Animations will still show to others if played locally, roblox handles player animations via a local script called "Animator". The requirement of a Server sided script for animations is if your animating a non-player character. Or not the local character.

0

Still wont work. I put them in their correct places, there is no errors in the output, but the animation doesn't play.

0

Wait, I forgot to make the animation id a real Animation.

0

Fixed it. Well thanks for the info Protogen but i'm playing the animation in the server just in case.

This discussion is preserved as it was. New answers, comments, and votes are closed.