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

The script does not accept a different key why?

If i replace uid_ with anything else it doesnt work anymore


local DSS = game:GetService("DataStoreService")

local datastore = DSS:GetDataStore("GeneralSaveData", "Players")

function generateDataKey(player)
    local ret = ("uid_" .. player.userId)
    return ret
end

function generateDataTable(player)
    local dataTable = {
        Units = player.leaderstats.Units.Value,
       	Level = player.leaderstats.Level.Value,
		Xp = player.leaderstats.Xp.Value
    }
    return dataTable
end

function saveDataForPlayer(player)
    local key = generateDataKey(player)
    local data = generateDataTable(player)
    datastore:SetAsync(key, data)
end

function inputDataToPlayer(player, data)
    player.leaderstats.Units.Value = data.Units
    player.leaderstats.Level.Value = data.Level
	player.leaderstats.Xp.Value = data.Xp
end

function loadDataForPlayer(player)
    local key = generateDataKey(player)
    local data = datastore:GetAsync(key)
    inputDataToPlayer(player, data)
end

game.Players.PlayerAdded:connect(function(player)
	player:WaitForChild("leaderstats")
	loadDataForPlayer(player) --Load first thing when they join
end)

game.Players.PlayerRemoving:connect(saveDataForPlayer) --Save data when the player leaves the game

2 comments

0

Well then the problem is something else. Please tell me the output.

0

also on line 6 you do not need to use the parentheses and you don't need spaces before or after the .. concatenator operation. Also you should not use deprecated code like :connect. You should use :Connect

1 answer

✓ Accepted answer

Because when you change the key that you saved the data with, you will never be able to access that data again unless the key is the same. Just like the key to a house. If I were to go buy a random key to replace my current one and then expect to be able to unlock my house I would be stupid. What you are doing is similar to getting a random key. You are making it impossible to load the data from the same "house" ever again unless you change the key back. When you save data with a "key" you are setting the locks of the "house" to only open to that key. So if you change the key it will not let you into that "house" again, but it will create a new "house" for you to store data in.

1 comment

0

The problem is not that the script does not load the old data but that it completely stops working

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