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

DataStore not saving a StringValue?

Hello! Basically I am creating a game with Cash and Jobs, and while testing the DataStores I have found a bug that my StringValue isn't saving to my datastore! Is their a bug in my Leaderstats code? There are no error outputs either.

ds = game:GetService("DataStoreService")
ds1 = ds:GetDataStore("RoLifeDataStoreForCash")
ds2 = ds:GetDataStore("RoLifeDataStoreForJobs")

game.Players.PlayerAdded:connect(function(p)
	local stats = Instance.new("Folder", p)
	stats.Name = "leaderstats"
	local money = Instance.new("IntValue", stats)
	money.Name = "Money"
	money.Value = ds1:GetAsync(p.userId) or 200
	local job = Instance.new("StringValue", stats)
	job.Name = "Job"
	job.Value = ds2:GetAsync(p.userId) or "None"
	wait(2)
	if job.Value == "Influencer" then
		game.ReplicatedStorage.rPhone:Clone().Parent = p.Backpack
	end
	
	job.Changed:connect(function(p)
		wait(0.2)
		ds2:SetAsync(p.userId, p.leaderstats.Job.Value)
	end)
end)

game.Players.PlayerRemoving:connect(function(p)
	ds1:SetAsync(p.userId, p.leaderstats.Money.Value)
end)

4 comments

1

Oh heck no, don't use changed and then save stuff, that is very unwise and ugly, Also don't use multiple datastores, also add bindtoclose, also, userId is deprecated, use UserId, also, "Instance.new("IntValue, stats) is deprecated

0

also don't use or "None"

0

Thank you mate, is it because I used multiple datastores? Or?

0

I don't see anything wrong with using or "None"

1 answer

✓ Accepted answer

Did you check the output (if in online mode, developer console is F9)? I believe you would have found an error there. If not, try adding print statements; before line 20 you might have:

print("job.Changed fired")
print("SetAsync with", p.userId, p.leaderstats.Job.Value)

I suspect that if you put those print statements in, the first one would run but the 2nd would error. If it errors silently, your only clue would be that the 2nd print doesn't run - you might then consider print(p), which would print "Value" I think.

The problem is that you don't want to receive an argument on line 19 - remove p or at least rename it.

Other things to improve (not strictly required if you don't intend to save anything else):

  • Use a single datastore and save multiple values in a single table
  • Auto-save data that's changed periodically (ex every 1-3 minutes) and also when they leave. Unless there's other code that prevents a player's job from changing frequently, you shouldn't save on job.Changed in case several people switch frequently.
  • Wrap all GetAsync and SetAsync calls in pcall and handle the cases when it errors (ex by retrying a couple times, maybe notifying the user if you can't load their data, etc).

1 comment

0

Thank you. :D

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