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

I dont know what is wrong. It says attempt to index nil with stop line 52?

The error is at line 52 where it attempts to index nil with stop yet stop is used before and there is no error there. Help?



local uis = game:GetService("UserInputService")

local crawlAnimation = script:WaitForChild("Crawl")
local loadedCrawlAnim

local crawlIdle = script:WaitForChild("CrawlIdle")
local loadedIdleAnim

local isCrawling = false

local humanoid = script.Parent:FindFirstChild("Humanoid")


uis.InputBegan:Connect(function(key, gameProcessed)
		
	if key.KeyCode == Enum.KeyCode.C then
				
		
		if isCrawling then
		
			isCrawling = false	
			
			if loadedCrawlAnim then loadedCrawlAnim:Stop() end	
			
			loadedIdleAnim:Stop()			
			
			game.ReplicatedStorage.OnCrouchBegun:FireServer(humanoid, 16, 50)
				
		elseif not isCrawling then

			isCrawling = true	
	
			loadedIdleAnim = humanoid:LoadAnimation(crawlIdle)
			loadedIdleAnim:Play()
			
			game.ReplicatedStorage.OnCrouchBegun:FireServer(humanoid, 3, 20)
		end
	end	
end)


game:GetService("RunService").RenderStepped:Connect(function()
		
	if not isCrawling then return end
		
	if humanoid.MoveDirection.Magnitude > 0 then
		
		if not loadedCrawlAnim then loadedCrawlAnim = humanoid:LoadAnimation(crawlAnimation) end
		if loadedCrawlAnim.IsPlaying == false then loadedCrawlAnim:Play() end	

	else
		loadedCrawlAnim:Stop()  --The error is here apparently yet stop works above--
		loadedIdleAnim:Play()
	end
end)

1 answer

Hi Yoshi,

The problem with your code, I believe, was that the if statements that made your loading of animations occur, never would be found true, or were never run, so that the animation was not set before it was called to be played.

The solution that I've used, unless there is some flaw I do not see, is to simply assign the variable to load the animation outside of the function, negating all of the if statement.

Here's the altered code I produced:

local uis = game:GetService("UserInputService")

local humanoid = script.Parent:WaitForChild("Humanoid")

local crawlAnimation = script:WaitForChild("Crawl")
local loadedCrawlAnim = humanoid:LoadAnimation(crawlAnimation) -- Assign out of variable

local crawlIdle = script:WaitForChild("CrawlIdle")
local loadedIdleAnim= humanoid:LoadAnimation(crawlIdle) -- Assign out of variable

local isCrawling = false




uis.InputBegan:Connect(function(key, gameProcessed)

    if key.KeyCode == Enum.KeyCode.C then


        if isCrawling then

            isCrawling = false  

            if loadedCrawlAnim then loadedCrawlAnim:Stop() end  
			
            loadedIdleAnim:Stop()           

            game.ReplicatedStorage.OnCrouchBegun:FireServer(humanoid, 16, 50)

        elseif not isCrawling then

            isCrawling = true   
			
			--Removed from here
            loadedIdleAnim:Play()

            game.ReplicatedStorage.OnCrouchBegun:FireServer(humanoid, 3, 20)
        end
    end 
end)


game:GetService("RunService").RenderStepped:Connect(function()

    if not isCrawling then return end

    if humanoid.MoveDirection.Magnitude > 0 then
		
		--Removed from here
        if loadedCrawlAnim.IsPlaying == false then loadedCrawlAnim:Play() end   

    else
        loadedCrawlAnim:Stop()
        loadedIdleAnim:Play()
    end
end)

This code worked with the animatioins I made for this, so if you have any troubles/queries, please do reply.

Hope this was of help.

2 comments

1

Thank you so much Ninja it worked without any bad output thanks to you, peace :)

0

Thanks, an accepted answer would be greatly appreciated :)

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