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

How can I save 2 Data's with DataStoreService?

I don't get any errors, but data is not saving :/

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local remote = game.ReplicatedStorage.eToPoints

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = leaderstats
	Points.Value = 0
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	Wins.Value = 0
	
	
	remote.OnServerEvent:Connect(function(player)
		player.leaderstats.Points.Value = player.leaderstats.Points.Value + 50
		player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 50
	end)
	
	
	local WinsData
	local Pointsdata
	local dataSaved, dataNotSaved = pcall(function()
		Pointsdata = PlayerData:GetAsync(player.UserId.."-Points")
		WinsData = PlayerData:GetAsync(player.UserId.."Wins")
	end)
	
	if dataSaved then
		Points.Value = Pointsdata
		Wins.Value = WinsData
		print("Got Data")
	else
		print(dataNotSaved)
	end	
end)



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

	local dataSaved, dataNotSaved = pcall(function()
		PlayerData:SetAsync(player.UserId.."-Points", player.leaderstats.Points.Value)
		PlayerData:SetAsync(player.UserId.."-Wins", player.leaderstats.Wins.Value)
	end)
	
	if dataSaved then
		print("Data saved")
	else
		print("Data not saved")
		wait(dataNotSaved)
	end
	
end)

1 comment

0

You're being slightly vague here. Explain what this whole script does or cut any lines if unecessary to your current problem. Can you elaborate more on your problem?

1 answer

First of all, you can use an Array to SetAsync instead of using SetAsync twice. Here's what I mean:

local dataTable = {
	local points = Points.Value
	local wins = wins.Value
}

PlayerData:SetAsync(plr.UserId,dataTable)

That's the basic explanation, and it saves your requests, especially when you need to do a lot of them. Secondly, the problem with Players.PlayerRemoving is that it doesn't save the last player's data in the server. To solve that, you can use game:BindToClose(). (Note: there's game:OnClose() as well, but it is deprecated.)

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