Script unable to find folder inside of leaderstats

So I’m unsure if I’m missing something obvious or not, but basically, I have a script that tries to get a datastored variable that’s inside of a folder inside of leaderstats. The problem is, I get a 12:44:40.984 PlayerValues is not a valid member of Player "Players.DDZ_76" - Server - Script:7 error on line 7.

The script that errors out:

game.Players.ChildAdded:Connect(function(new)
	local character = new.Character
	local LeaderStats = Instance.new("Folder", new)
	LeaderStats.Name = "LeaderStats"
	local stat = Instance.new("IntValue", LeaderStats)
	stat.Name = "Words"
	stat.Value = new.LeaderStats.PlayerValues.PreviousWords.Value + math.random(1, 50) --Error 
	
	LeaderStats.PlayerStats.PreviousWords.Value = stat.Value

	new.Chatted:Connect(function(message)
		local words = string.split(message, " ")
		stat.Value -= #words
		if stat.Value <= 0 then new:Kick() end
	end)
	
	local gui = script.TextName:Clone()
	local check = game.Workspace:WaitForChild(new.Name)
	if check~=nil then
		local find = check:FindFirstChild("Head")
		if find ~= nil then	
			gui.Parent = find
			gui.Namer.Text = "Words Left: " .. new.LeaderStats.Words.Value
			new.LeaderStats.Words.Changed:Connect(function()
				gui.Namer.Text = "Words Left: " .. new.LeaderStats.Words.Value
			end)
		end
	end
end)

Script that creates the other folder (PlayerValues folder):

local DataStore = game:GetService("DataStoreService"):GetDataStore("SaveMyData")

game.Players.PlayerAdded:Connect(function(plr)

	local data = DataStore:GetAsync(plr.UserId)

	if(data) then

		for i, v in pairs(data) do 
			local PlayerValues = Instance.new("Folder")
			PlayerValues.Name = "PlayerValues"
			PlayerValues.Parent = plr.LeaderStats


			local Cash = Instance.new("IntValue")
			Cash.Name = "Cash"
			Cash.Value = v["Cash"]
			Cash.Parent = PlayerValues

			local TotalWords = Instance.new("IntValue")
			TotalWords.Name = "TotalWords"
			TotalWords.Value = v["TotalWords"]
			TotalWords.Parent = PlayerValues

			local PreviousWords = Instance.new("IntValue")
			PreviousWords.Name = "PreviousWords"
			PreviousWords.Value = v["PreviousWords"]
			PreviousWords.Parent = PlayerValues


		end
	else 
		local PlayerValues = Instance.new("Folder")
		PlayerValues.Name = "PlayerValues"
		PlayerValues.Parent = plr.LeaderStats

		local Cash = Instance.new("IntValue")
		Cash.Name = "Cash"
		Cash.Value = 0
		Cash.Parent = PlayerValues

		local TotalWords = Instance.new("IntValue")
		TotalWords.Name = "TotalWords"
		TotalWords.Value = 0
		TotalWords.Parent = PlayerValues

		local PreviousWords = Instance.new("IntValue")
		PreviousWords.Name = "PreviousWords"
		PreviousWords.Value = 0
		PreviousWords.Parent = PlayerValues

	end


end)



game.Players.PlayerRemoving:Connect(function(plr)
	local saveddata = {}

	saveddata[plr.UserId] = {
		Cash = plr.LeaderStats.PlayerValues:FindFirstChild("Cash").Value,
		TotalWords = plr.LeaderStats.PlayerValues:FindFirstChild("TotalWords").Value,
		PreviousWords = plr.LeaderStats.PlayerValues:FindFirstChild("PreviousWords").Value
	}

	DataStore:UpdateAsync(plr.UserId, function(oldValue)
		return saveddata
	end)

end)

Use a WaitForChild when you access the leaderstats and also the folder needs to be exactly named leaderstats or it won’t show in the leaderboard.

3 Likes

You should wait until said folder is added like @nonamehd24 said. The folder is made in a separate script, it might not be created yet when you try to access it.

This is unrelated but why are you using ChildAdded? You should use PlayerAdded which I see you are using in your datastore script.

1 Like

Oh I just noticed that, a friend of mine wanted to add the datastores to his script.

1 Like