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