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?

Asked by 6 years ago

So i try to make player animate when taken damage.

local dmg = true
local ready = true
local animation =(2488441216)


script.Parent.Touched:Connect(function(hit)
   if hit.Parent.Humanoid and dmg == true and ready then
    local hum = hit.Parent.Humanoid
    hum:TakeDamage(10)
    local track = hum:LoadAnimation(animation)
    track:Play()
        dmg = false
        wait(1)
        dmg = true
    end
end)

How to fix this?

Error:Unable to cast value to Object

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I think when your suppose to use :LoadAnimation(), you need to include the the rbxassetid:// part before the ID so it'll return a bad cast. Also use :FindFirstChildOfClass() or :FindFirstChild() for checking if theres something in a object before getting it in the object.

local dmg = true
local ready = true
local animation ="rbxassetid://"..tostring(2488441216)

script.Parent.Touched:Connect(function(hit)
   if hit.Parent:FindFirstChildOfClass("Humanoid") and dmg == true and ready then
    local hum = hit.Parent.Humanoid
    hum:TakeDamage(10)
    local track = hum:LoadAnimation(animation)
    track:Play()
        dmg = false
        wait(1)
        dmg = true
    end
end) 

0
Addition the script.Parent is script and Script.Parent.Parent is ServerScriptService. btw how am i going to use :FindFirstChildOfClass() or :FindFirstChild() Pdpieee 1 — 6y
0
no for like checking if a humanoid exists User#23365 30 — 6y
0
is part line 3 and 4 an error? Pdpieee 1 — 6y
0
oops i forgot to add that in lol User#23365 30 — 6y
View all comments (8 more)
0
hold up' User#23365 30 — 6y
0
Ok Pdpieee 1 — 6y
0
ok done User#23365 30 — 6y
0
New error. FindFirstChild is not a valid member of a model. Pdpieee 1 — 6y
0
I replace it with :FindFirstChild but the Unable to cast value to Object still poping up Pdpieee 1 — 6y
0
o wait User#23365 30 — 6y
0
you need to make the Instance "Animation" to use :LoadAnimation() User#23365 30 — 6y
0
then define the animation in the arguments and set the animation ID User#23365 30 — 6y
Ad

Answer this question