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

attempt to index nil with 'WaitForChild'?

Asked by 3 years ago
Edited 3 years ago

Hey I've been getting this error and I tried every method to fix it but it doesn't work.

Image where the parent is: http://imgur.com/a/8BibY5T

Error line:

local Stable = script.Parent:WaitForChild("Stable")

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local PlayerGui = Player:WaitForChild("PlayerGui")

local Stable = script.Parent:WaitForChild("Stable")
local Content = Stable:WaitForChild("Content")
local Template = Content:WaitForChild("Container"):WaitForChild("Template")

local Stores = workspace:WaitForChild("Stores")
local StablesFolder = Stores:WaitForChild("Stables")
local Permissions = ReplicatedStorage:WaitForChild("Permissions")
local HorsePerms = require(Permissions:WaitForChild("Horse"))

local Range = 5

local NPCs = {}
local Alive = true
local ActiveObj = nil
local ActiveType = nil

function AddNPC(Object, Type)
    print('added npc' .. Object.Name)
    if Object:IsA("Model") and NPCs[Object] == nil and Object:IsDescendantOf(StablesFolder) then
        if Object:FindFirstChild("NPC") and Object.NPC:FindFirstChild("Detector") then
            NPCs[Object] = Type
            print('NPC LOADED')
        end
    end
end

script.Parent.Enabled = true
Stable.Position = UDim2.new(-0.4, 0, 0, 0)
Stable.Visible = true

wait(2)

-- Stables
for _,Child in pairs(StablesFolder:GetChildren()) do
    AddNPC(Child, "Stable")
end
StablesFolder.ChildAdded:connect(function(Child)
    AddNPC(Child, "Stable")
end)

-- Detect when player dies
Humanoid.Died:connect(function()
    Alive = false
end)

if HorsePerms:Allowed(Player) == true then
    Content.Headings.Visible = false
    local HorseButton = Template:Clone()
    HorseButton.Parent = Content.Container
    HorseButton.Visible = true
    HorseButton.Name = "Horse1"

    local Debounce = false
    HorseButton.MouseButton1Click:connect(function()
        if not Debounce then
            Debounce = true
            if ReplicatedStorage:FindFirstChild("Events") and ReplicatedStorage.Events:FindFirstChild("StablesRequest") then
                ReplicatedStorage.Events:FindFirstChild("StablesRequest"):FireServer(ActiveObj)
                wait(5)
            else
                warn("Failed to find StablesRequest")
            end
            Debounce = false
        end
    end)
else
    Stable.NoHorseWarning.Visible = true
    Content.Headings.Visible = false
end

while wait(1) do
    for Model,Type in pairs(NPCs) do
        if Model ~= nil and Model:FindFirstChild("NPC") and Model.NPC:FindFirstChild("Detector") and Player:DistanceFromCharacter(Model.NPC.Detector.Position) <= Range then
            if Alive then
                if ActiveObj ~= Model then
                    ActiveObj = Model
                    ActiveType = Type
                    if script.Parent:FindFirstChild(ActiveType) then
                        if game.StarterGui:GetCore("ChatActive") then
                            ChatWasOpen = true
                        end
                        game.StarterGui:SetCore("ChatActive", false)
                        script.Parent:FindFirstChild(ActiveType):TweenPosition(UDim2.new(0.1, 0, 0, 0), "Out", "Quad", 0.3)
                    end
                end
            end
        else
            if ActiveObj == Model then
                if script.Parent:FindFirstChild(ActiveType) then
                    script.Parent:FindFirstChild(ActiveType):TweenPosition(UDim2.new(-0.4, 0, 0, 0), "In", "Quad", 0.3)
                end
                ActiveObj = nil
                ActiveType = nil
                if ChatWasOpen then
                    game.StarterGui:SetCore("ChatActive", true)
                    ChatWasOpen = false
                end
            end
        end
    end
end
0
What is the parent? iHavoc101 127 — 3y
0
sorry, forgot to post image. https://imgur.com/a/8BibY5T Versvop 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

You Should use PlayerGui instead of script.Parent

local Stable = Player:WaitForChild("PlayerGui").Businesses.Stable

If that doesnt work, let me know

Ad

Answer this question