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

'Unable to cast value to object' error... can anyone help?

Asked by 3 years ago

so im trying to make it so that when I press 'x' it'll change the walk speed as well as play a run animation i made, but i get an error saying 'Unable to cast value to Object' and it's pointing at line 15

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local sprinting = false
local sprint = script.SprintAnim

if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

wait()

local hum = character:WaitForChild("Humanoid")

mouse.KeyDown:connect(function(key)
    local sprintPlay = hum:LoadAnimation("SprintAnim")
    if key == "x" then
        if sprinting then
            sprinting = false
            sprintPlay:Stop()
        else
            sprinting = true
            sprintPlay:Play()
        end
        game.ReplicatedStorage.SprintToggle:FireServer(sprinting)
    end
end)

ps: this is a local script

1 answer

Log in to vote
0
Answered by 3 years ago

You should be passing an Animation to Humanoid:LoadAnimation(), not a string.

Also Humanoid:LoadAnimation() is deprecated and you should use Animator:LoadAnimation().

Ad

Answer this question