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

Attempt to index nil with findfirstchild?

i am new to scripting and i was wondering why it said attempt to index nil with find first child when i typed this in.the problem is on the bold line.This is a regular script.

local stats = game.ServerScriptService.LeaderStats:FindFirstChild("leaderstats")

local Pmoney = stats:findFirstChild("Cash")

local Pmulti = stats:findFirstChild("Multi")

local Preb = stats:findFirstChild("Rebirths")

so i was wondering if any of you guys can help me. sorry if this is a easy solution im very new to scripting and barely know anything.

1 comment

0

Do :WaitForChild(), as the objects might not be added when the script run

1 answer

Well stats does not exist or the script executed before they loaded.

Solutions:

Use :WaitForChild(), it waits for the instance to load. Example:

local stats = game.ServerScriptService:WaitForChild"LeaderStats":WaitForChild("leaderstats")

	 local Pmoney = stats:WaitForChild("Cash")
	 local Pmulti = stats:WaitForChild("Multi")
	 local Preb = stats:WaitForChild("Rebirths")

Other solution is that it just does not exist, check if you defined it right, for example it could be named "leaderStats" but you defined it as "LeaderStats" the L is capital so it could not find anything or it is located in ServerStorage but you typed in ServerScriptService

Last solution is that if it is a local script, you can not access serverscriptservice with it. There is replicatedStorage for it.

Hope this helps.

1 comment

0

Yes this has worked thank you!

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