Issue referencing existing object

I’m trying to store a players leaderstat (gold) via datastores, however my script cannot seem to find the leaderstats folder that is in the player. To solve this, I’ve tried direct referencing, wait for child, and findfirstchild, but the script keeps erroring.

The error that pops up when using direct referencing is “leaderstats is not a valid player of Player Player.ModdedDreams”, and when using WaitForChild() it times out. When in game I can easily find the leaderstats folder in my player.
Datastore code:

local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
local Players = game:GetService("Players")




Players.PlayerAdded:Connect(function(plr)
	local playerUserID = plr.userId
	local playerGold = plr:WaitForChild("leaderstats").Gold.Value --This is where the code errors
	Players.PlayerRemoving:Connect(function(plr)
		local setSuccess, errorMessage = pcall(function()
			goldStore:SetAsync(playerUserID, playerGold)
		end)
		if not setSuccess then
			local errorMessage = "Error - Datastore attempt failed"
			warn(errorMessage)
		end

		Players.PlayerAdded:Connect(function(plr)
			local getSuccess, currentGold = pcall(function()
				return goldStore:GetAsync(playerUserID)
			end)
			if getSuccess then
				print('Yay!')
				plr:FindFirstChild("leaderstats").Gold.Value = currentGold
				local oop = plr:FindFirstChild("leaderstats").Gold.Value
				print(oop)
			end
		end)
	end)
end)

The code used to create the leaderstats:

  local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    
    local gold = Instance.new("IntValue")
    gold.Name = "Gold"
    gold.Value = 0
    gold.Parent = leaderstats
    
    local strength = Instance.new("IntValue")
    strength.Name = "Strength"
    strength.Parent = leaderstats
    strength.Value = 0

    
    local level = Instance.new("IntValue")
    level.Name = "Rebirths"
    level.Value = 0
    level.Parent = leaderstats
    
    print("Setup Complete!")
    leaderstats.Parent = plr

There are some errors in your code. Try to use this instead:

local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local playerUserID = plr.UserId
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
		
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
		gold.Value = 0
	gold.Parent = leaderstats
		
	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Parent = leaderstats
	strength.Value = 0

	local level = Instance.new("IntValue")
	level.Name = "Rebirths"
	level.Value = 0
	level.Parent = leaderstats
		
	print("Setup Complete!")
	leaderstats.Parent = plr
	
	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(playerUserID)
	end)
	if getSuccess then
		print('Yay!')
		leaderstats.Gold.Value = currentGold
		local oop = plr:FindFirstChild("leaderstats").Gold.Value
		print(oop)
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local setSuccess, errorMessage = pcall(function()
		goldStore:SetAsync(plr.UserId, plr.leaderstats.Gold.Value)
	end)
	if not setSuccess then
		local errorMessage = "Error - Datastore attempt failed"
		warn(errorMessage)
	end
end)

REMEMBER: Put the Script within the ServerScriptService

Thank you for your help, however while there are no errors, it does not seem to be setting the value of the gold leaderstat to the one stored. Any ideas?

Could you show the output, please?

20:35:31.794 :arrow_forward: cloud_142613350.EmptyScriptAdder:7: Incomplete statement: expected assignment or a function call (x2) - Studio
20:35:35.280 Hello world! - Client - LocalScript:1
20:35:35.518 Mode: rotate - Server
20:35:35.644 Setup Complete! - Server - Script:36
20:35:35.786 Mode: rotate - Client

Could you show the entire script?

local Players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local leaderstats = Instance.new("Folder")


game.Players.PlayerAdded:Connect(function(plr)
	local welcome = script.welcome:Clone()
	welcome.Parent = plr.PlayerGui
	wait(5)
	welcome:Destroy()

	
	

	leaderstats.Name = "leaderstats"
	
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats
	
	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Parent = leaderstats
	strength.Value = 0

	
	local level = Instance.new("IntValue")
	level.Name = "Rebirths"
	level.Value = 0
	level.Parent = leaderstats
	
	print("Setup Complete!")
	leaderstats.Parent = plr
	
	local goldcount = script.ScreenGui.Frame.GoldCount
	goldcount.Text = gold.Value.. ""
	local clone = script.ScreenGui:Clone()
	clone.Parent = plr.PlayerGui
	while true do
		gold.Value = gold.Value + 10
		wait(10)

	end
	while true do
		clone.Frame.Goldcount.Text = gold.Value
		wait(11)
	end
end)



local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
local Players = game:GetService("Players")




Players.PlayerAdded:Connect(function(plr)
	local playerUserID = plr.userId
	local playerGold = leaderstats:WaitForChild("Gold").Value --This is where the code errors
	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(playerUserID, leaderstats.Gold.Value)
	end)
	if getSuccess then
		print('Yay!')
		leaderstats.Gold.Value = currentGold
		
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local setSuccess, errorMessage = pcall(function()
		goldStore:SetAsync(plr.UserId, plr.leaderstats.Gold.Value)
	end)
	if not setSuccess then
		local errorMessage = "Error - Datastore attempt failed"
		warn(errorMessage)
	end
end)```

Could you show the explorer tab??

Players folder while game is running, and serverscriptservice folder while game is stopped.

Could you show the MainScript's children?

Why don’t you put MainScript's children within the StarterGui and then show me their descendants?

You’re attempting to just create 1 Instance of the leaderstats variable, it’s not creating 1 every time a player joins and it being parented to the player’s object

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")

Players.PlayerAdded:Connect(function(Player)
    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"
    stats.Parent = Player

	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats
	
	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Parent = leaderstats
	strength.Value = 0

	local level = Instance.new("IntValue")
	level.Name = "Rebirths"
	level.Value = 0
	level.Parent = leaderstats

    local goldcount = script.ScreenGui.Frame.GoldCount
    goldcount.Text = gold.Value
    local clone = script.ScreenGui:Clone()
    clone.Parent = plr.PlayerGui

    while true do
        gold.Value += 10
        clone.Frame.Goldcount.Text = gold.Value
        wait(10)
    end

    local Data
    local success, whoops = pcall(function()
        Data = goldStore:GetAsync(Player.UserId)
    end)

    if success and Data then
        print("Yes")
        gold.Value = Data
    end
end)

Players.PlayerRemoving:Connect(function(Player)
    local success, whoops = pcall(function()
        goldStore:SetAsync(Player.UserId, Player.leaderstats.Gold.Value)
    end)

    if success then
        print("Yes")
    else
        warn(whoops)
    end
end)

I also went ahead and fixed a couple of other unnecessary things inside your script

Thank you however all the variables dependant on the leaderstats variable are now broken, what should i do?

You cannot do this

while true do end yields infinitely and the rest of the code won’t work

Whoops

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")

Players.PlayerAdded:Connect(function(Player)
    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"
    stats.Parent = Player

	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats
	
	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Parent = leaderstats
	strength.Value = 0

	local level = Instance.new("IntValue")
	level.Name = "Rebirths"
	level.Value = 0
	level.Parent = leaderstats

    local goldcount = script.ScreenGui.Frame.GoldCount
    goldcount.Text = gold.Value
    local clone = script.ScreenGui:Clone()
    clone.Parent = plr.PlayerGui

    local Data
    local success, whoops = pcall(function()
        Data = goldStore:GetAsync(Player.UserId)
    end)

    if success and Data then
        print("Yes")
        gold.Value = Data
    end

    while Players:GetPlayerByUserId(plr.UserId) do
	    gold.Value += 10
	    clone.Frame.Goldcount.Text = gold.Value
    	wait(10)
    end

end)

Players.PlayerRemoving:Connect(function(Player)
    local success, whoops = pcall(function()
        goldStore:SetAsync(Player.UserId, Player.leaderstats.Gold.Value)
    end)

    if success then
        print("Yes")
    else
        warn(whoops)
    end
end)

while true do end will continue infinitely, even if the player is not on the server. So, check every time if the player is still on the server:

while Players:GetPlayerByUserId(plr.UserId) do
	gold.Value += 10
	clone.Frame.Goldcount.Text = gold.Value
	wait(10)
end
2 Likes

Final code?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")

Players.PlayerAdded:Connect(function(Player)
	local welcome = script.welcome:Clone()
	welcome.Parent = Player.PlayerGui
	spawn(function()
		wait(5)
		welcome:Destroy()
	end)

	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = Player

	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats
	
	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Parent = leaderstats
	strength.Value = 0

	local level = Instance.new("IntValue")
	level.Name = "Rebirths"
	level.Value = 0
	level.Parent = leaderstats

	local goldcount = script.ScreenGui.Frame.GoldCount
	goldcount.Text = gold.Value
	local clone = script.ScreenGui:Clone()
	clone.Parent = Player.PlayerGui
	
	local Data
	local success, whoops = pcall(function()
		Data = goldStore:GetAsync(Player.UserId)
	end)

	if success and Data then
		print("Yes")
		gold.Value = Data
	end

	while Players:GetPlayerByUserId(Player.UserId) do
		gold.Value += 10
		clone.Frame.Goldcount.Text = gold.Value
		wait(10)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local success, whoops = pcall(function()
		goldStore:SetAsync(Player.UserId, Player.leaderstats.Gold.Value)
	end)

	if success then
		print("Yes")
	else
		warn(whoops)
	end
end)

The code seems fine however for some reason the data is not being stored upon player leave, resulting in no data being loaded.

add this?

game:BindToClose(function()
	wait() --Add a time (maximum: 30 seconds)
end)