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

DataStore Script Is Not Working Anymore After New Addition to Script?

I decided I would add a "Volume" value to my data storages and that is what broke everything. Where the comment says "eerror", savedValueKOs is not nil and savedValueVolume is nil. I decided that there is no DataStore for Volume? If there isn't, how do I create a new one? Would appreciate any help thank you!

local DataStore = game:GetService("DataStoreService"):GetDataStore("KOs")
local DataStore2 = game:GetService("DataStoreService"):GetDataStore("Volume")

game.Players.PlayerAdded:connect(function(player)
	
	local key = "Player_" .. player.UserId
	
	local savedValueKOs = DataStore:GetAsync(key)
	local savedValueVolume = DataStore2:GetAsync(key)
	
	if savedValueKOs and savedValueVolume then -- eerror
		
		--Save format: {points, coins}
		player.leaderstats.KOs.Value = savedValueKOs[1]
		player.Character.Volume.Value = savedValueVolume[1]
	else
		local KillsToSave = {player.leaderstats.KOs.Value}
		DataStore:SetAsync(key, KillsToSave)
		local VolumesToSave = {player.Character.Volume.Value}
		DataStore:SetAsync(key,VolumesToSave)
	end
	
end)

game.Players.PlayerRemoving:connect(function(player)
	
	local key = "Player_" .. player.UserId
	
	--Save key: {points, coins}
	local KillsToSave = {player.leaderstats.KOs.Value}
	DataStore:SetAsync(key, KillsToSave)
	local VolumesToSave = {player.Character.Volume.Value}
	DataStore:SetAsync(key,VolumesToSave)
	
end)

4 comments

1

correct me if I'm wrong, but on line 20 and 33m you have "DataStore:SetAsync" when saving the Volume, which is your "KOs". Shouldn't it be like: DataStore2:SetAsync

0

How did I miss that I'll check it out soon thanks

1

^ Did it fix?

0

nope, but you helped me fix one part of the problem

1 answer

✓ Accepted answer

As iamnoamesa has said the problem is that you are using the same data store still effectively overwriting the saved data.

There is also another issue with this design, you have now doubled the number of requests needed to save and load player data. The main point of using a table is to save more than one piece of data instead of splitting the data which requires more requests.

You can the data to the table:-

local saveData = {player.leaderstats.KOs.Value, player.Character.Volume.Value}

-- format two 
local saveData = {['ko'] = player.leaderstats.KOs.Value, ['vol'] = player.Character.Volume.Value}

If you do choose to change the save format you will need to load the data in differently. It is also a good idea to not use multiple data stores for the same game, simply change the key if you need to store data in another key as this is a more structured design.

Example not including error management:-

local DataStore = game:GetService("DataStoreService"):GetDataStore("test") -- It is best to make this something relevant to the game e.g. testingPlace1, FPS ect

-- simplify the save process
local function saveData(key, saveData)
	DataStore:SetAsync(key, saveData)
end

game.Players.PlayerAdded:connect(function(player)

    local key = "Player_" .. player.UserId

    local plrData = DataStore:GetAsync(key)

    if plrData then -- eerror
        --Save format: {points, coins}
        player.leaderstats.KOs.Value = plrData[1] or 0
        player.Character.Volume.Value = plrData[2] or 0
    else
		--Save key: {points, coins}
		saveData(key, {player.leaderstats.KOs.Value, player.Character.Volume.Value})
    end

end)

game.Players.PlayerRemoving:connect(function(player)
	--Save key: {points, coins}
	saveData("Player_" .. player.UserId, {player.leaderstats.KOs.Value, player.Character.Volume.Value})
end)

I hope this helps, please comment if you do not understand how / why this code works.

1 comment

0

omg... thank you! I have been trying to fix my glitch for days...

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