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

Data Saving based on the other IntValue?

So I have 2 values, they are just int values, without leaderstats. Which value i save last via Server Side, the other value is going to be set to that value too.

Eg if i do theCash's value to 100 then I do theWins's Value to 500

When i reload the game, both of them will be 500, why?

local DS = game:GetService("DataStoreService"):GetDataStore("a")
game.Players.PlayerAdded:Connect(function(player)
	local theCash = Instance.new("IntValue",player)
	theCash.Name = "theCash"
	theCash.Value = DS:GetAsync(player.UserId) or 0
	
	local theWins = Instance.new("IntValue", player)
	theWins.Name = "theWins"
	theWins.Value = DS:GetAsync(player.UserId) or 0
	
	local function saveTheData(theData)
		theData.Changed:Connect(function()
			DS:SetAsync(player.UserId, theData.Value)
		end)
	end
	
	saveTheData(theCash)
	saveTheData(theWins)

end)




2 answers

First of all that Instance.new constructor is deprecated, second you should always use pcall to for network calls, as they can fail ans which would reset someone points to zero here. Also you dont need here an inner function

And the reason both would be 500 is because you use the same key to store the values.

you are overwriting the cash value with the wins value

Solution use different keys


local DS = game:GetService("DataStoreService"):GetDataStore("a")

local function saveTheData(key, theData)
        theData.Changed:Connect(function()
            DS:SetAsync(key, theData.Value)
        end)
    end

game.Players.PlayerAdded:Connect(function(player)
    local theCash = Instance.new("IntValue",player) --bad
    theCash.Name = "theCash"
    theCash.Value = DS:GetAsync(player.UserId.."-cash") or 0 -- no pcall

    local theWins = Instance.new("IntValue", player)
    theWins.Name = "theWins"
    theWins.Value = DS:GetAsync(player.UserId.."-wins") or 0

    saveTheData(player.UserId.."-cash", theCash)
    saveTheData(player.UserId.."-wins", theWins)

end)

4 comments

0

Also note, this way you are doing a lot of requests, you will hit the limits, and it will in turn start queueing your setasyncs which could result in dataloss

0

but if instance.new is bad, why you wrote it in the code too

0

cuz i just showed you the solution for different keys

You're saving two int values in the same key of the data store, look:

local DS = game:GetService("DataStoreService"):GetDataStore("a")
game.Players.PlayerAdded:Connect(function(player)
    local theCash = Instance.new("IntValue",player)
    theCash.Name = "theCash"
    theCash.Value = DS:GetAsync(player.UserId) or 0 -- You're getting from the data store DS + user ID

    local theWins = Instance.new("IntValue", player)
    theWins.Name = "theWins"
    theWins.Value = DS:GetAsync(player.UserId) or 0 -- You're getting from the data store DS + user ID

    local function saveTheData(theData)
        theData.Changed:Connect(function()
            DS:SetAsync(player.UserId, theData.Value)
        end)
    end

    saveTheData(theCash)
    saveTheData(theWins)

end)



What I recommend doing is, atributting a aditional key to the user ID, that defines if its cash or wins, see:

local DS = game:GetService("DataStoreService"):GetDataStore("a")
game.Players.PlayerAdded:Connect(function(player)
    local theCash = Instance.new("IntValue",player)
    theCash.Name = "theCash"
    theCash.Value = DS:GetAsync(player.UserId.. "-cash") or 0 -- You're getting from the data store DS + user ID + cash key

    local theWins = Instance.new("IntValue", player) 
    theWins.Name = "theWins"
    theWins.Value = DS:GetAsync(player.UserId.."-wins") or 0-- You're getting from the data store DS + user ID + wins key


    local function saveTheData(theData)
        theData.Changed:Connect(function()
            DS:SetAsync(player.UserId, theData.Value)
        end)
    end

    saveTheData(theCash)
    saveTheData(theWins)

end)

game.Players.PlayerRemoved:Connect(function(player)
       local theCash = player.theCash.Value
       local theWins = player.theWins.Value

       DS:SetAsync(player.UserId.."-cash", theCash)
       DS:SetAsync(player.UserId.."-wins", theWins) -- Setting async the values and its respective keys.
end)

1 comment

0

I tried the code and it didnt save the values, when i restart the game they go to 0

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