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

Attempt to index nil with findfirstchild?

Asked by 4 years ago
Edited 4 years ago

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.

0
Do :WaitForChild(), as the objects might not be added when the script run PoWerofThEePg 43 — 4y

1 answer

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
4 years ago

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.

0
Yes this has worked thank you! babtrick 0 — 4y
Ad

Answer this question