Attempted to index nil with ‘WaitForChild’

I was testing my notification module when I received an error “Attempt to index nil with WaitForChild”

The error occurs on line 5

Can anyone fix this! Thanks


local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui",4)
local Notifications = PlayerGui:WaitForChild("Notifications")
local Container = Notifications:WaitForChild("Container")
local SFX = ReplicatedStorage:WaitForChild("SFX")
local Template = script:WaitForChild("NotifTemplate")
local SoundEffect = SFX:WaitForChild("Notify")
local ORIGINAL_SIZE = Template.Size
local NotificationService = {}
function NotificationService:Notify(TopText: string, BottomText: string, IconId: string, Duration: number)
        
        local newNotification = Template:Clone()
        newNotification.Size = UDim2.fromScale(0,0)
        
        newNotification.BackgroundTransparency = 1
        newNotification.TopText.Transparency = 1
        newNotification.BottomText.Transparency = 1
        newNotification.Icon.Transparency = 1
        newNotification.TopText.Text = TopText
        newNotification.BottomText.Text = BottomText
        newNotification.Icon.Image = IconId
        
        newNotification.Parent = Container
        
        TweenService:Create(newNotification, TweenInfo.new(0.3), {BackgroundTransparency = 0}):Play()
        TweenService:Create(newNotification.TopText, TweenInfo.new(0.3), {TextTransparency = 0}):Play()
        TweenService:Create(newNotification.BottomText, TweenInfo.new(0.3), {TextTransparency = 0}):Play()
        TweenService:Create(newNotification.Icon, TweenInfo.new(0.3), {ImageTransparency = 0}):Play()
        
        TweenService:Create(newNotification, TweenInfo.new(.8, Enum.EasingStyle.Back, Enum.EasingDirection.Out), {Size = ORIGINAL_SIZE}):Play()
        
        SoundEffect:Play()
        
        task.wait(Duration)
        
        TweenService:Create(newNotification, TweenInfo.new(0.2), {BackgroundTransparency = 1}):Play()
        TweenService:Create(newNotification.TopText, TweenInfo.new(0.3), {TextTransparency = 1}):Play()
        TweenService:Create(newNotification.BottomText, TweenInfo.new(0.3), {TextTransparency = 1}):Play()
        TweenService:Create(newNotification.Icon, TweenInfo.new(0.3), {ImageTransparency = 1}):Play()
        TweenService:Create(newNotification, TweenInfo.new(.8, Enum.EasingStyle.Elastic, Enum.EasingDirection.Out), {Size = UDim2.fromScale(0,0)}):Play()

1 Like

Which line is erroring? (extra characters)

Make sure you are referring to the correct object. Also tell us where the error is.

So sorry! Line 6 is erorring extra characters

PlayerGui is the only query in this script that has a timeout, so I would assume that it is the one returning nil. The PlayerGui is parented by the engine, so there is no guarantee it will ever load in cases such as where the character is never added, or the client is being slow.

This would be a cheap fix for my standards, but if you are positive PlayerGui is always supposed to be loaded in the game, just wait for it without a timeout. It’s better to get an infinite yield warning on a slow device and then let is suddenly load instead of it never loading at all.

1 Like

How would I implement this into the module? and I’m so stupid the error is on line 5 because there is no space

By simply removing the timeout argument from the WaitForChild call to PlayerGui, but I have to disclaim that this is really a deeper problem with the engine quirks and there isn’t a specifically best solution. Removing the timeout won’t make the problem of the PlayerGui not always existing go away, but it’s the easiest method to patch over the error and thus I mentioned it.

Is it being ran on the client?

1 Like

It’s being ran on a module script

This did not fix the problem as line 5 is still erroring with the same error

is the module script being ran by a client script

is your script which is calling the module a server or a local script? The error sais that the local player is nil . Local player can only be use with local script :slight_smile:

Seems like it’s being ran by a server script and can’t get the player, try using local Player = nil Players.PlayerAdded:Connect(function(player) Player = player end) Syntax might be wrong because I typed it on my phone.

Yes extra characters extra extra

It’s being ran In a module extra char

Thank you so much I wasn’t thinking straight!

1 Like

I had a remote being handled on the server calling it!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.