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

Datastore is not actually saving the player data?

I want the data to be saved ever 6.5 seconds or when the player leaves and I am getting the prints so I know it is running but it is not actually saving the data.

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("TestData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Model")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = leaderstats
	
	local size = Instance.new("NumberValue")
	size.Name = "Size"
	size.Value = 1.07
	size.Parent = leaderstats
	
	ds1:SetAsync(player.UserId, cash.Value, size.Value)
	--[[cash.Changed:Connect(function()
		print("Saving Data")
		ds1:SetAsync(player.UserId, cash.Value, size.Value)
		print(player.UserId.."'s Data of "..cash.Value.." "..cash.Name.." has been saved!")
	end)--]]
	while true do
		wait(6.5)
		print("Saving Data")
		ds1:SetAsync(player.UserId, cash.Value, size.Value)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	ds1:SetAsync(player.UserId, player.leaderstats.Cash.Value, player.leaderstats.Size.Value)
	print(player.UserId.."'s Data of "..player.leaderstats.cash.Value.." "..player.leaderstats.cash.Name.." has been saved!")
end)

1 answer

If you want to save two values, you can use one datastore and store both values in an array, or use two different datastores to save each value. Also, for your own error handler, you can wrap the function in a pcall.

You can save your data in one datastore like this:

--Creating a table
local array = {cash.Value; size.Value}

--our error handler

local success, message=pcall(function()
        ds1:SetAsync(player.UserId, array)
end)
if success then
       print("Successfully saved!")
else
       warn("Failed to save "..player.Name.."'s data: "..message)
end

1 comment

0

It is still not saving but I am not getting any errors

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