Attempt to index nil with 'ChildAdded'

I wanted to make it so there would be viewmodels when you choose a tool,but for some reason it doesn’t work and says “Players.plato2002.PlayerScripts.Viewmodels:31: attempt to index nil with ‘ChildAdded’”,i couldn’t really find anything on the devforum.

Here’s the script (i’ve marked the error):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local Offsets = require(ReplicatedStorage.ViewModelOffsets)
local Camera = workspace.CurrentCamera
local char = game.Players.LocalPlayer.Character

local function makeaviewmodel(Name)
	if game:GetService("ReplicatedStorage").ViewModels[Name] then
		
		local ClonedModel = game:GetService("ReplicatedStorage"):WaitForChild("ViewModels")[Name]:Clone()

		ClonedModel.Parent = Camera
		RunService.RenderStepped:Connect(function()
			if ClonedModel ~= nil and Offsets[ClonedModel.Name] then
				if ClonedModel:FindFirstChild("HumanoidRootPart") ~= nil then
					ClonedModel:SetPrimaryPartCFrame(Camera.CFrame * Offsets[ClonedModel.Name])
				end
			end
		end)
		if ClonedModel.Humanoid:LoadAnimation(ClonedModel.Idle).IsPlaying == false then
			ClonedModel.Humanoid:LoadAnimation(ClonedModel.Idle):Play()
		end
		
	end	

	
end


char.ChildAdded:Connect(function(child) -- ERROR HERE
	if child:IsA("Tool") then
		makeaviewmodel(child.Name)
	end

end)



How can i fix it?
Thanks in advance.

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

Additionally, you have a memory leak.

1 Like

I have no idea what it is,where can i get some information about it?

Memory leak is a global programming term, you can find it anywhere.
In short, a memory leak in computer software occurs when unneded memory is not released, which continues to consume allocated memory. A program can only use as much memory as it requests, any point past that will cause the program to crash, or memory swapping.

In Roblox, a Script Connection (known as RBXScriptConnection or the :Connect() method) takes up memory. Each time you create a connection it consumes some memory. When not used, it must be disconnected. It’s not bad if you have 10 connections lying around that barely get used (however not recommeded) but you have a very important leak here:

local function makeaviewmodel(Name)
	if game:GetService("ReplicatedStorage").ViewModels[Name] then
		
		local ClonedModel = game:GetService("ReplicatedStorage"):WaitForChild("ViewModels")[Name]:Clone()

		ClonedModel.Parent = Camera
		RunService.RenderStepped:Connect(function()

See, every time you call makeaviewmodel() you’re creating a new RenderStepped connection, every time. Seeing as this is a local script, it would result in something horrendous. To fix this, we can simply:

  1. Use a loop instead of RunService, or
  2. Disconnect the function when we’re done.

To disconnect a function, we first have to set it as a variable:

local connection = RunService.Heartbeat:Connect(function())

Once we’re done using that connection, we simply disconnect it:

connection:Disconnect()

Note that the function inside will continue to run, so you can call it anytime. However you can’t use this connection again, so you’ll have to establish a new one.

You can’t ignore a memory leak. Once spotted, get to fixing it.

2 Likes

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