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

data not saving when i play in game?

pls help. it prints "data saved" when i test my game with two players on studio, but when i join the game and reset my character, and then join back my death won't save.

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")
wait(5)

game.Players.PlayerAdded:connect(function(player)
	
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local kills = Instance.new("IntValue",leaderstats)
	kills.Name = "Kills"
	
	local deaths = Instance.new("IntValue",leaderstats)
	deaths.Name = "Deaths"
	
	
	local data
	local success, errormsg = pcall(function()
		data = myDataStore:SetAsync(player.UserId,player.leaderstats.Deaths.Value)
	end)
		
	if success then
		player.leaderstats.Deaths.Value = data
		print("data loaded")
	else
		warn(errormsg)
		print("Data not saved")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, errormsg = pcall(function()
	myDataStore:SetAsync(plr.UserId,plr.leaderstats.Deaths.Value)
	end)
	
	if success then
		print("Data saved")
	else
		print("Data not saved")
		warn(errormsg)
	end
end)

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function()
		local humanoid = plr.Character:WaitForChild("Humanoid")
		local deaths = plr.leaderstats.Deaths
		humanoid.Died:connect(function()
			deaths.Value = deaths.Value + 1
		end)
	end)
end)

3 comments

0

Line 9 should be :GetAsync(player.UserId) instead of :SetAsync(player.UserId), for getting the stats, you don't need to define the value as it's returned as a table. And you can add your characterAdded, death stuff in the first player added under the 2nd to last end.

0

works now, thank you! also one more question, how would i save multiple values at once? e.g. saving kills and deaths

0

nvm i read your code i'll try to put it into a table

1 answer

✓ Accepted answer

Error 1

Line 9 Should be :GetAsync(), instead of :SetAsync()

:GetAsync() is to get the stored data for the player.

:SetAsync() is to set the new values for the player.

These aren't really errors, but just tips.

For getting the stats, you don't need to define the value as it's returned as a table, so you would have to index the table if you have multiple values.

And you can add your characterAdded, death stuff in the first player added under the 2nd to last end

This is how I would do it, even though I saw an error and a few improvements.

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")
wait(5)

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

    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local kills = Instance.new("IntValue",leaderstats)
    kills.Name = "Kills"

    local deaths = Instance.new("IntValue",leaderstats)
    deaths.Name = "Deaths"

    local data
    local success, errormsg = pcall(function()
        data = myDataStore:GetAsync(player.UserId)
    end)

    if success then
        player.leaderstats.Deaths.Value = data[1] or 0
        player.leaderstats.Kills.Value = data[2] or 0 --// Edited
        print("Data loaded")
    else
        warn(errormsg)
        print("Data not saved")
    end

    player.CharacterAdded:connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        local deaths = player.leaderstats.Deaths
        humanoid.Died:connect(function()
            deaths.Value = deaths.Value + 1
        end)
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)

    local dataToSave = {
        plr.leaderstats.Deaths.Value,
        plr.leaderstats.Kills.Value --// Edited
    }

    local success, errormsg = pcall(function()
        myDataStore:SetAsync(plr.UserId,dataToSave)
    end)

    if success then
        print("Data saved")
    else
        print("Data not saved")
        warn(errormsg)
    end
end)

Hope this helped and worked! From my experience, DataStores have been janky very recently.

2 comments

0

how would I save multiple values? i am struggling on how to use a table to save kills and deaths

0

I edited the table on line 43, and I edited line 23 to load the data. You can check how I did it.

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