Problem with getting player's leaderstats

So I am having a little bit of a issue with a Simple server player time script, the issue being that it does not wan’t to display the actual text on the surface gui’s text.


Script:

game.Players.PlayerAdded:Connect(function(Player)
	local template = game.ReplicatedStorage.TimeTemplate
	
	template:FindFirstChild("Playername").Text = Player.Name
	template:FindFirstChild("TimePlayed").Text = Player.leaderstats.Time_Played.Value
	
	template:Clone().Parent = workspace.ServerTimePart.DonationGui.Main
	
	while wait(1) do
		Player.leaderstats.Time_Played.Value = Player.leaderstats.Time_Played.Value + 1
	end
end)

Error:
10:33:08.097 leaderstats is not a valid member of Player "Players.DaffyDavinko" - Server - ServerTime:3

Could you show us the script where you create the leaderstats?

leaderstats probably wasn’t created in time. When was it instantiated?

1 Like

You can create leaderstats in the same script too.

The leaderstats we’re initially made in another script, which handled data saving too, and it was basically a simple leaderstat script but just with the loading functions and stuff.

Basically here it is.

-- DonationBoardManager.lua
-- DaffyDavinko
-- 24 April, 2021


-- // Services
local Players				= game:GetService("Players")
local ReplicatedStorage		= game.ReplicatedStorage
local DataService 			= game:GetService("DataStoreService")

-- // Variables
local LocalData = DataService:GetDataStore("DonationData")

-- // Functions
local function LoadData(Player)
	local DonatedValue = Instance.new("NumberValue")
	local TimePlayed = Instance.new("NumberValue")
	local leaderstats = Instance.new("Folder")
	
	local Template = game.ReplicatedStorage.DonationTemplate.Template:Clone()
	local TimePlayedTemp = game.ReplicatedStorage.TimeTemplate:Clone()
	
	Template.Parent = workspace.DonationBoardPart.DonationGui:WaitForChild("Main")
	TimePlayedTemp = workspace.ServerTimePart.DonationGui:WaitForChild("Main")
	
	leaderstats.Parent = Player
	DonatedValue.Parent = leaderstats
	TimePlayed.Parent = leaderstats	
	
	leaderstats.Name = "leaderstats"
	DonatedValue.Name = "Donated_Amount"
	TimePlayed.Name = "Time_Played"
	
	
	
	local PlayerID = Player.UserId
	local Data

	local Success, Error = pcall(function()
		Data = LocalData:GetAsync(PlayerID)
	end)

	if Success and Data ~= nil then
		DonatedValue.Value = Data.DonatedValue
		
		Template.Playername.Text = Player.Name
		Template.DonationAmount.Text = Data.DonatedValue
	else
		warn("DEBUG: Player_Data is nil!")
	end
end

local function SaveData(Player)
	local PlayerID = Player.UserId

	local Data = {
		DonatedValue = Player.leaderstats.Donated_Amount.Value,
		TimePlayed = Player.leaderstats.Time_Played.Value,
	}

	local Success, Error = pcall(function()
		LocalData:SetAsync(PlayerID, Data)
	end)

	if Success then
		print("DEBUG: Saved data to Player")
	else
		warn("DEBUG: An error has occurred while saving the data")
	end
end

-- // Events
Players.PlayerAdded:Connect(LoadData)
Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		SaveData(Player)
	end
end)

In that case try using Player:WaitForChild(‘leaderstats’) instead of Player.leaderstats maybe?

Tried it before but it doesn’t work and it doesn’t even output anything if I use WaitForChild.

Try using :FindFirstChild(“leaderstats”)

Also didn’t post something like “infinite yield possible” in output?

1 Like

No its not doing absolutely anything in the output.

Tried this before, and still doesn’t work and doesn’t output anything.

put a print function right below where you create the leaderstats folder in the other script, then check if it prints before printing the error

Place print() calls between lines to see where your script stops. It’s also easier to control your work by using variables to define a location once to avoid error.

local leaderStats = player:WaitForChild("leaderstats")
local timePlayed = leaderStats:WaitForChild("Time_Played")
...

It’ll be easier to read and understand later on when you want to read it. This will be confusing for you to look back at in the future the way it is.

Revisioned:

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Player:WaitForChild("leaderstats")
	local timePlayed = leaderstats:WaitForChild("Time_Played")
	local template = game.ReplicatedStorage.TimeTemplate
	
	template:FindFirstChild("Playername").Text = Player.Name
	template:FindFirstChild("TimePlayed").Text = tostring(timePlayed.Value)
	
	template:Clone().Parent = workspace.ServerTimePart.DonationGui.Main
	
	while wait(1) do
		if Player then -- so it breaks when a player leaves
			timePlayed.Value += 1
		else break end
	end
end)
1 Like

So its still not really working.

Did you get an error in output if you pasted my code directly? I made a variable naming mistake. I edited that word. Also please use print() s to find where this issue lies.

An example is:

print "checkpoint 1"
local Part = Instance.new("Part")
print "checkpoint 2"
Part.Name = "Hello"
print "checkpoint 3"
Part.Size = UDim2.new(0.5,0,0.5,0) -- this should error it expects a vector
print "checkpoint 4"
Part.Position = Vector3.new(0.5,0.5,0.5)
print "checkpoint end"

Expected output:

checkpoint 1
checkpoint 2
checkpoint 3

So now we know where the script failed