Why am I getting these errors?

Im trying to look for difference instances throughout the player Gui, but I keep receiving errors that causes it to not work.

Error 1

Error:

  12:06:52.466  Players.NubblyFry.PlayerGui.ScreenGui.CashMoney.Change:1: attempt to index nil with 'FindFirstChild'  -  Client - Change:1
  12:06:52.467  Stack Begin  -  Studio
  12:06:52.467  Script 'Players.NubblyFry.PlayerGui.ScreenGui.CashMoney.Change', Line 1  -  Studio - Change:1
  12:06:52.467  Stack End  -  Studio

Script:

local CashMoney = script.Parent.Parent.Parent:FindFirstChild("leaderstats"):FindFirstChild("Cash Money")

if CashMoney == script.Parent.Parent.Parent:FindFirstChild("leaderstats"):FindFirstChild("Cash Money") then
	print("Successful")
else
	error("Cash script was unsuccessful and erorred.")
end

function ScaleX()
	for i = 1,4 do
		task.wait(0.01)
		script.Parent.Size = script.Parent.Size + UDim2.new(0.01, 0, 0, 0)
	end

	for i = 1,4 do
		task.wait(0.01)
		script.Parent.Size = script.Parent.Size - UDim2.new(0.01, 0, 0, 0)
	end
end

function ScaleY()
	for i = 1,2 do
		task.wait(0.01)
		script.Parent.Size = script.Parent.Size + UDim2.new(0, 0, 0.01, 0)
	end

	for i = 1,2 do
		task.wait(0.01)
		script.Parent.Size = script.Parent.Size - UDim2.new(0, 0, 0.01, 0)
	end
end

CashMoney:GetPropertyChangedSignal("Value"):Connect(function(value)
	if value then
		script.Parent.Text = "Cash Money: "..tostring(value)
		ScaleX()
		ScaleY()
	end
end)
Error 2

Error:

  12:06:55.143  XP is not a valid member of TextLabel "Players.NubblyFry.PlayerGui.ScreenGui.TopBar.Level"  -  Server - XpDataStore:31
  12:06:55.143  Stack Begin  -  Studio
  12:06:55.143  Script 'ServerScriptService.XpDataStore', Line 31  -  Studio - XpDataStore:31
  12:06:55.143  Stack End  -  Studio

Screenshot 2022-07-12 123853

Script:

-- DataStore for XP

local DataStoreService = game:GetService("DataStoreService")

local RepStor = game:GetService("ReplicatedStorage")

local xpDataStore = DataStoreService:GetOrderedDataStore("xpDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats2 = Instance.new("Folder")
	leaderstats2.Name = "leaderstats2"
	leaderstats2.Parent = player
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = leaderstats2
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Parent = leaderstats2

	local xpData
	local levelData
	
	local success, errormessage = pcall(function()
		xpData = xpDataStore:GetAsync(player.UserId.. "-XP")
		levelData = xpDataStore:GetAsync(player.UserId.. "-Level")
	end)

	if success then
		player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").TopBar.Level.XP = xpData
		
		player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").TopBar.Level.Level = levelData
	else
		print("There was an error whilest loading your data!")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		xpDataStore:SetAsync(player.UserId.."-XP", player.leaderstats2:FindFirstChild("XP").Value)
		xpDataStore:SetAsync(player.UserId.."-Level", player.leaderstats2:FindFirstChild("Level").Value)
	end)
	
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error while saving data!")
		warn(errormessage)
	end
end)

I think that should be:
script.Parent.Parent.Parent.Parent

I would suggest changing the way you refer to your objects. Right now you’re using script.Parent.Parent.Parent, you’re better off referring to your leaderstats like this:

-- Services
local Players = game:GetService('Players')
local Player = Players.LocalPlayer

local leaderstats = Player:FindFirstChild('leaderstats')
local CashMoney
if leaderstats then
	CashMoney = leaderstats:FindFirstChild('Cash Money')
end

Agreed, but I assumed that he is using a server script, hence my reply.

1 Like

Understandable assumption, my bad. In that case they can refer to the player using:

local Player = script:FindFirstAncestorOfClass('Player')

As the script is inside the PlayerGui

1 Like

Like this?

local Player = script:FindFirstAncestorOfClass('Player')
local CashMoney = Player:FindFirstChild("leaderstats"):FindFirstChild("Cash Money")

Also Im using a local script for both of them.

Oh, well in that case then just do:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local CashMoney = Player:FindFirstChild("leaderstats"):FindFirstChild("Cash Money")

I would personally make a variable for leaderstats to make sure that object exists first.

Another good practice is to use WaitForChild in case your code runs before the leaderstats object is created.

In that case you can just use the LocalPlayer variable to refer to the player, but both ways work

-- Using LocalPlayer
local Player = game:GetService('Players').LocalPlayer

-- Using FindFirstAncestorOfClass
local Player = script:FindFirstAncestorOfClass('Player')

Result:

local Player = game:GetService('Players').LocalPlayer
local leaderstats = Player:WaitForChild('leaderstats')

local CashMoney
if leaderstats then
    CashMoney = leaderstats:WaitForChild('Cash Money')
end
1 Like