dataStoreService issues as a confused person

i know this is such bad practice and janky but at heart why doesnt it work?

hi, i am trying to write a script that would just remember a value ( dollars ) even after the player rejoins, and have been testing it by playing , then going into the code and changing the value of the intvalue created.

No exceptions are raised, but it is not working as intended

i know this is such bad practice and janky but at heart why doesnt it work?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local database = DataStoreService:GetDataStore("data")



local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local dollars = Instance.new("IntValue")
	dollars.Name = "dollars"
	dollars.Parent = leaderstats
	
	print(database:GetAsync(player.UserId))
	
	dollars.Value = database:GetAsync(player.UserId)	
	
end

local function onPlayerRemoving(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local dollars = leaderstats:WaitForChild("dollars")
	print(player , player.UserId)
	print(dollars.Value)
	database:SetAsync(player.UserId,dollars.Value)
	
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

thanks for all the help

2 Likes

Are you sure the script is running?
Add a print somewhere to make sure
Also ensure it’s a server script

when you say server script do you mean if it is in the server script service?

if so yes. idk why it doesnt work :sleepy:

Ok firstly wrap all Get and SetAsyncs in a pcall.
Next, try not to load or save too much, it can cause requests to be dropped.

If you have any errors, reply to this question with them.

You also need to make sure that if the loaded value is nil, change it to 0 or some number as values can’t take nil as an input.

He means is it a normal script or is it a local script

You should be using a normal script Not local.

Also do you have studio api services enabled for datastores? This needs to be enabled in the setting of your game.

yea its in a normal script and studio api services is enabled

1 Like

are you getting any errors? and have u tried my ssolutions

1 Like

id start by pcalling the retrieval of data

local success, data = pcall(function()
database:GetAsync(player.UserId)
end)

if success and data ~= nil then
      dollars.Value = data
end

other than that, i dont really see any issues. any errors in console? also make sure that the script isnt a local script!!

could you explain how to do that or give a link to roblox documentation?

i have just wrapped the retrieval in a pcall function like shown
no errors in console , success = true but data is nill, so its not setting “dollars” to the data
i have not used pcall function like this i have always seen it as:

local success , errorMsg = pcall(function()
      print("bla bla")
end)

does it work differently now? what are the paremeters

The code you provided works for me on a newly created baseplate – so it seems that there isn’t any actual errors with the datastore code itself.

I would guess to try and see if the data saves & loads on a live server in your game; if it still doesn’t save/load there, I would assume there could be another script interfering with the saving process.

The code below uses pcall to output warnings if there are any.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local database = DataStoreService:GetDataStore("data")

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local dollars = Instance.new("IntValue")
	dollars.Name = "dollars"
	dollars.Parent = leaderstats
	
	local success,plrdollars = pcall(function()  -- Instead of calling GetAsync twice, we'll store it in a variable.
		return database:GetAsync(player.UserId)  
	end)
	if not success then
		warn("Data Get Failed",plrdollars) -- plrdollars becomes the error message/response.
		return
	end
	
	print("Got dollars", plrdollars)
	dollars.Value = plrdollars
end

local function onPlayerRemoving(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local dollars = leaderstats:WaitForChild("dollars")
	print(player , player.UserId)
	print(dollars.Value)
	
	local success,response = pcall(function() 
		database:SetAsync(player.UserId,dollars.Value) 
	end)
	if not success then
		warn("Data Save Failed",response)
	else
		print("Saved!")
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
1 Like

i have enabled the api thing on a new baseplate and have tried to run it. after loading in, i changed the int value of “dollars” of my character to a random number manually ( e.g going into the object explorer) and then leaving. when i rejoined i did not have the same amount of dollars as last session, no errors were raised. there shouldnt be another script interfering as this is a new base plate. here is the output window
image
it did have this error tho … but idk if its related or helpful. this might have been an error from getting the value/ setting the value, but it did not appear every time i tested it

thank you for the notes on your version of the script :grinning:

You’re welcome!

Oh that warning is just from one of Roblox’s internal CoreScripts

I think I know what the issue is now though – Are you changing the value on the explorer while in Client view? If so, the changes aren’t replicated to the server which would explain why it’s not saving your data changes.

Press this to toggle between client and server view.
image
Once you are in server view, you can edit the dollars value there and hopefully it should save

Sometimes datastore saving does not actually work solely on studio (I have experienced this myself a couple times) so try publishing the game and testing it on the actual website rather than studio.

2 Likes

i do the data ~= nil bit so that loading data once retrieved can be caught if nil. if it is nil, it will catch it and just make the value 0 (which is what usually happens when a player joins for the first time unless you want them to start with x amount, if so just write an elseif to check if nil, then set amount you want.) it doesnt work much differently, it just has the inbuilt check of if data is not nil

if i did that do would there be a way to change the intvalue in the game? that script is the only script in the game, nothing else can change it

IT WORKED i feel very silly now

thank you so much
today is my victory day this was not fun

1 Like

Yeah you can use the command bar on the server-tab ijn the developer cosnole.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.