Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why Does My Data Store Come Out With No Error Messages?

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)

2 answers

Datastores do not work in Studio. Try in the actual game client

2 comments

0

Yes I have, It still doesn't save the data

0

actually it does work in studio

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.

3 comments

0

also the api reference states: DataStoreService cannot be used in Studio if a game is not configured to allow access to API services. See the Data Stores article for instructions.

0

Article: https://developer.roblox.com/en-us/articles/Data-store (it at the bottom of the article btw)

0

I already have that enabled

This discussion is preserved as it was. New answers, comments, and votes are closed.