I am new to roblox studio and Lua in general. I have been trying to make a currency system for days but I cannot figure it out. I believe my ServerStorage script works properly but the amount of money shown on the in game GUI does not save and always restarts back at 0.

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
	print('player joined')
	
	local Dollas = Instance.new('IntValue')
	Dollas.Name = 'Dollas'
	Dollas.Parent = leaderstats
	
	local playerUserId = 'Player Id: '..player.UserId
	local data = PlayerData:GetAsync(playerUserId)
	
	if data then
		Dollas.Value = data
	else
		Dollas.Value = 0
	end
end

local function onPlayerLeave(player)
	local success, err = pcall(function()
		local playerUserId = 'Player Id: '..player.UserId
		PlayerData:SetAsync(playerUserId, player.leaderstats.Dollas.Value)
		print('player left')
	end)
	if not success then
		print('player not saved')
		warn('Could not save data')
	end
end

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

4 comments

0

Are you testing in roblox studio?

0

I tried in studio with API enabled. Another issue I've been having is that my game just will not publish when I click publish. I made it Public as well but I still can't even see it there

1

Press alt p to publish your game, also use BindToClose as well to save the data.

0

thank ya sir it work. I'd upvote but my reputation is too low whatever that means