So, I'm currently trying to make a similar game to cookie clicker, I made this simple leader stats that simply adds a "Cookie" value, which is self-explanatory, and it saves when the player leaves, and then loads when the player joins, however, it is not working, there is no error in the output, so I'm guessing its a logical error, I used Tech with Mike's leader stats tutorial, so if anyone wants to view it here's the link: https://www.youtube.com/watch?v=-wAJAI95ivs, help would be appreciated!

Script:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cookies = Instance.new("IntValue")
	Cookies.Name = "Cookies"
	Cookies.Parent = leaderstats
	
	local playerUserId = "Player_".. player.UserId
	local data = playerData:GetAsync(playerUserId)
	
	if data then
		Cookies.Value = data
	else
		Cookies.Value = 0
	end
end

local function onPlayerExit(player)
	local success, err = pcall(function()
		local playerUserId = "Player_"..player.UserId
		playerData:SetAsync(playerUserId, player.leaderstats.Cookies.Value)
	end)
	if not success then
		warn("Could not save data")
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

2 comments

0

Is API services enabled?

0

Yes.