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

How come one of my IntValues aren't saving?

Hi, I'm trying to make it save 2 IntValues in leaderstats. I've gotten it to save points, but not gems. What am I doing wrong? Thanks

local DSService = game:GetService("DataStoreService"):GetDataStore("SaveStats")
game.Players.PlayerAdded:connect(function(player)
	-- Define variables
	local uniquekey = "id-"..player.userId
	local leaderstats = Instance.new("IntValue", player)
	local points =  Instance.new("IntValue")
	local gems = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	points.Parent = leaderstats
	points.Name = "Points"
	gems.Parent = leaderstats
	gems.Name = "Gems"
		
	
	-- GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		points.Value = GetSaved[1]
		gems.Value = GetSaved[2]
	else
		DSService:SetAsync(uniquekey, points.Value, gems.Value)
	end
end)
	
game.Players.PlayerRemoving:connect(function(player)
local uniquekey = "id-"..player.userId
local pointtable = {player.leaderstats.Points.Value}
local gemtable = {player.leaderstats.Gems.Value}
DSService:SetAsync(uniquekey, pointtable, gemtable)	
	
end)

1 comment

0

you can only save one value per SetAsync, so you either do SetAsync(UniqueKey,{Pointtable,Gemtable}), or you do two SetAsyncs. (I recommend the first one.)

2 answers

I believe SetAsync only takes 2 arguments. The key, and the data. The gems will not save because that argument is unused and ignored by the function. I would use a table instead, eg { Points: points, Gems: gems } then you can access it using GetSaved.Points or GetSaved.Gems.

You spelt UserId wrong lmao

also i dont think you do these in a table and im not sure u do "id-" before the userid.

local uniquekey = player.UserId
local pointtable = player.leaderstats.Points.Value
local gemtable = player.leaderstats.Gems.Value
DSService:SetAsync(uniquekey, pointtable, gemtable) 

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