Attempt to call a nil value when checking if it is nil

My code spits out an error while playing an animation when it’s nil, but I check for it is being nil and returns false, that it is nil.

My code:

if CurrentAnimation ~= nil then CurrentAnimation:Stop() end

I have tried to do:

if CurrentAnimation then CurrentAnimation:Stop() end

But got no where, I would appreciate some help! Thanks.

1 Like

Can you send the error so we could figure out what’s wrong.

Check in the code to ensure that you did not assign that variable to anything else other than a animation track. Also, send the entire scope in which that variable was used and set, your current post is vague.

I don’t think you would get an error for checking a value of a variable.

Is CurrentAnimation an Animation object or AnimationTrack? If it’s an Animation, it might return nil because :Stop() isn’t a valid function so it is trying to call nil.

Otherwise we need more resources to find out what is going on

image
Sorry for late response.

After I check if it’s nil I printed the classname, and it returned nil.

@tralalah’s assessment seems right to me unless there is something you aren’t sharing–
copy and paste more / the actual code.

Can you show us the code in the script around line 82?

When I encounter this problem I use “WaitForChild()”

Sure.

You can’t really use WaitForChild() on it’s own self.

You should use it on its parent

With the current situation, this wouldn’t work/make sense.

send the place where you called the function.

1 Like

Errrrm, Sorry there is too much stuff that is important in this place. But I figured our why (Kinda)
Before the if statement it isn’t nil, and while the if statement is happening is does become nil. I’m going to keep trying to find a solution.

1 Like

No. Show the code that calls AssaultRifle.PlayAnimation()

Well the only way this can happen is if your calling the function with the incorrect params, also in the future if your not comfortable with sharing your code then it makes no sense to ask for help, your post is very vague and you can’t expect no-one to help.

I fixed it. I just removed it and it didn’t have a impact on anything. Basically useless code. Sorry for wasting your guy’s time!