Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

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

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. Protogen_Dev 268 — 2y
0
Still wont work. I put them in their correct places, there is no errors in the output, but the animation doesn't play. MrBucketOfficial 39 — 2y
0
Wait, I forgot to make the animation id a real Animation. T3_MasterGamer 2189 — 2y
0
Fixed it. Well thanks for the info Protogen but i'm playing the animation in the server just in case. T3_MasterGamer 2189 — 2y
Ad

Answer this question