Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to fix attempt to index nill parent?

Asked by 3 years ago

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

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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)
0
What I mean is crouch is nil because there isn't an existing function yet. Just put the function script above line 1 Skydoeskey 108 — 3y
0
so u mean put the whole script? for function crouch() AviaFlyHigh 15 — 3y
0
Watch the edit of my awsner Skydoeskey 108 — 3y
Ad

Answer this question