I am trying to expand my coding knowledge by (attempting) to scripting a commonly used scripting feature, data stores. I have searched up data stores on the roblox dev hub and still come out with an un-working, no error message script. Help would be greatly appreciated.
Code:
local DataStoreService = game:GetService('DataStoreService') local GoldCurrency = DataStoreService:GetDataStore('PlayersGold') game.Players.PlayerAdded:Connect(function(player) local PlayerID = player.UserId local leaderstats = Instance.new("Folder") leaderstats.Name = 'leaderstats' leaderstats.Parent = player local Gold = Instance.new("IntValue") Gold.Name = 'Gold' Gold.Parent = leaderstats local success, currentExperience = pcall(function() return GoldCurrency:GetAsync(PlayerID) end) if success then Gold.Value = GoldCurrency else Gold.Value = 0 end end) game.Players.PlayerRemoving:Connect(function(player) local PlayerID = player.UserId local success, err = pcall(function() GoldCurrency:SetAsync(PlayerID, player.leaderstats.Gold.Value) end) if success then print("It Worked!") else print(err) end end)
Datastores do not work in Studio. Try in the actual game client
I've encountered this problem before, and fixed it by realizing i have to enable API Services (in game settings) first, otherwise it will not work.