Leaderstats is not a valid member of Player

I don’t understand why it keeps giving me this error code, I couldn’t find anything wrong myself.

local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local krons = Instance.new("IntValue")
	krons.Name = "Krons"
	krons.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = experienceStore:GetAsync(player.UserId.."currency")
	end)
	
	if success then
		krons.Value = data
	else
		print("Error while data load")
		warn(errormessage)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	
	local success, errormessage = pcall(function()
		experienceStore:SetAsync(player.UserId.."-Krons",player.leaderstats.Krons.Value)
	end)
	
	if success then
		print("Player Data Saved")
	else
		print("Failed Save")
		warn(errormessage)
	end
	
end)

image

I think you should use PlayerRemoving instead of PlayerAdded event or is it on purpose ?

game.Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	local Krons = leaderstats and leaderstats:FindFirstChild("Krons")
	
	if Krons then
		local success, errormessage = pcall(function()
			experienceStore:SetAsync(player.UserId.."-Krons", Krons.Value)
		end)
		
		if success then
			print("Player Data Saved")
		else
			print("Failed Save")
			warn(errormessage)
		end
	end
end)

Yeah that’s all that messed up everything

yeah I didn’t notice lol, newbie mistake

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.