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)