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

Unable to cast value to Object?

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

✓ Accepted answer

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) 

12 comments

0

Addition the script.Parent is script and Script.Parent.Parent is ServerScriptService. btw how am i going to use :FindFirstChildOfClass() or :FindFirstChild()

0

no for like checking if a humanoid exists

0

is part line 3 and 4 an error?

0

oops i forgot to add that in lol

0

hold up'

0

Ok

0

ok done

0

New error. FindFirstChild is not a valid member of a model.

0

I replace it with :FindFirstChild but the Unable to cast value to Object still poping up

0

o wait

0

you need to make the Instance "Animation" to use :LoadAnimation()

0

then define the animation in the arguments and set the animation ID

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