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

How to fix attempt to index nill parent?

Ok so I am getting an error when I type in the console it tells me this!

Error:

14:04:22.163 - script.Parent.Crouch.MouseButton1Click:Connect(function()
14:04:22.164 - 	crouch()
14:04:22.165 - end):1: attempt to index nil with 'Parent'

Code:

script.Parent.Crouch.MouseButton1Click:Connect(function()
	crouch()
end)

Full code:

local player = game.Players.LocalPlayer

local userInputService = game:GetService("UserInputService")

local animationID = "rbxassetid://04979694208"

local animPlaying = false

function crouch()
	if player:FindFirstChild("Contestant") then
			if animPlaying == false then
				local animObj = Instance.new("Animation")
				animObj.AnimationId = animationID
				local loadedAnim = player.Character.Humanoid:LoadAnimation(animObj)
				loadedAnim:Play()
				player.Character.Humanoid.WalkSpeed = 10
				animPlaying = true
				script.Parent.Crouch.ImageColor3 = Color3.fromRGB(26, 144, 255)
				script.Parent.Crouch.TextLabel.BackgroundColor3 = Color3.fromRGB(26, 144, 255)
				print("Playing")
			else
				print("Stopping")
				for i , v in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
					v:Stop()
				end
				player.Character.Humanoid.WalkSpeed = 16
				animPlaying = false
				script.Parent.Crouch.ImageColor3 = Color3.fromRGB(255,255,255)
				script.Parent.Crouch.TextLabel.BackgroundColor3 = Color3.fromRGB(255,255,255)
			end
		end	
	end

userInputService.InputBegan:Connect(function(input,gpe)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		print("Corect input")
		crouch()
	end
end)

script.Parent.Crouch.MouseButton1Click:Connect(function()
	crouch()
end)

game.ReplicatedStorage.ToggleCrouch.OnClientEvent:Connect(function(visibility)
	print("ToggleCrouch picked up visibility "..tostring(visibility))
	script.Parent.Crouch.Visible = visibility
	for i , v in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
		v:Stop()
	end
	player.Character.Humanoid.WalkSpeed = 16
	animPlaying = false
	script.Parent.Crouch.ImageColor3 = Color3.fromRGB(255,255,255)
	script.Parent.Crouch.TextLabel.BackgroundColor3 = Color3.fromRGB(255,255,255)
end)

1 answer

It's because crouch() is an unknown function. Scripts run from line 1 to line x. So to fix this issue. Simply have function crouch() at the top of your script


function crouch()
    if player:FindFirstChild("Contestant") then
            if animPlaying == false then
                local animObj = Instance.new("Animation")
                animObj.AnimationId = animationID
                local loadedAnim = player.Character.Humanoid:LoadAnimation(animObj)
                loadedAnim:Play()
                player.Character.Humanoid.WalkSpeed = 10
                animPlaying = true
                script.Parent.Crouch.ImageColor3 = Color3.fromRGB(26, 144, 255)
                script.Parent.Crouch.TextLabel.BackgroundColor3 = Color3.fromRGB(26, 144, 255)
                print("Playing")
            else
                print("Stopping")
                for i , v in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
                    v:Stop()
                end
                player.Character.Humanoid.WalkSpeed = 16
                animPlaying = false
                script.Parent.Crouch.ImageColor3 = Color3.fromRGB(255,255,255)
                script.Parent.Crouch.TextLabel.BackgroundColor3 = Color3.fromRGB(255,255,255)
            end
        end 
    end

script.Parent.Crouch.MouseButton1Click:Connect(function()
    crouch()
end)

Full code:

local player = game.Players.LocalPlayer

local userInputService = game:GetService("UserInputService")

local animationID = "rbxassetid://04979694208"

local animPlaying = false


userInputService.InputBegan:Connect(function(input,gpe)
    if input.KeyCode == Enum.KeyCode.LeftControl then
        print("Corect input")
        crouch()
    end
end)

script.Parent.Crouch.MouseButton1Click:Connect(function()
    crouch()
end)

game.ReplicatedStorage.ToggleCrouch.OnClientEvent:Connect(function(visibility)
    print("ToggleCrouch picked up visibility "..tostring(visibility))
    script.Parent.Crouch.Visible = visibility
    for i , v in pairs(player.Character.Humanoid:GetPlayingAnimationTracks()) do
        v:Stop()
    end
    player.Character.Humanoid.WalkSpeed = 16
    animPlaying = false
    script.Parent.Crouch.ImageColor3 = Color3.fromRGB(255,255,255)
    script.Parent.Crouch.TextLabel.BackgroundColor3 = Color3.fromRGB(255,255,255)
end)

3 comments

0

What I mean is crouch is nil because there isn't an existing function yet. Just put the function script above line 1

0

so u mean put the whole script? for function crouch()

0

Watch the edit of my awsner

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