Need help with datastore

Hello , i really need someone to help , i have 3 scripts of datastore but two of them are just same . Dataloss still occur and i dont want to spend my time on datastore for too long . Im willing to pay

2 Likes

Can you send these scripts and errors

2 Likes

Helo Alex , heres the script !

Money data :

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataSaver001") --
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats

	local success, data = pcall(function()
		return DataStore:GetAsync(player.UserId.."-Money")
	end)

	if success then
		Money.Value = data or 3000
		print("Money Data Loaded")
	else
		warn("Error while loading Money data: " .. data)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, error = pcall(function()
		DataStore:SetAsync(player.UserId.."-Money", player.leaderstats.Money.Value)
		print("Money Data Saved")
	end)

	if success then
		print("Player data saved successfully!")
	else
		warn("Error while saving Money data: " .. error)
	end
end)

while true do
	wait(500) 
	for _, player in ipairs(game.Players:GetPlayers()) do
		local success, error = pcall(function()
			DataStore:SetAsync(player.UserId.."-Money", player.leaderstats.Money.Value)
			print("Money Data Saved")
		end)

		if not success then
			warn("Error while saving Money data: " .. error)
		end
	end
end


game:BindToClose(function()
	task.wait(10)
end)

And dealership data :

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData") --PlayerData
local debounceTable = {}

local cars = {
-- list cars put here ok 
	"HondeEX5",
	"HondeWave100",
	"Kasaki 150sp",
	"Lagenda115zr"
}

local function createCarValue(player, name, value)
	local carValue = Instance.new("BoolValue")
	carValue.Name = name
	carValue.Value = value
	carValue.Parent = player:WaitForChild("OwnedCars")
	return carValue
end

local function onPlayerJoin(player) 
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "OwnedCars"
	leaderstats.Parent = player

	local data = playerData:GetAsync("Player_" .. player.UserId)
	if data then
		for _, car in ipairs(cars) do
			if data[car] == true then
				print(data)
				createCarValue(player, car, true)
			end
		end
		print("Data Loaded")
	else
		-- ouh i seee
		-- ni tk perlu letak sebab klo new player dia akan create semua bool
	--[[	for _, car in ipairs(cars) do
			print(data)
			print(2)
			createCarValue(player, car, false) --> bool tu akan jdi false
		end]] 
		print("New user or new motorcycle")
	end
end

local function createTable(player)
	local playerStats = {}
	for _, stat in pairs(player.OwnedCars:GetChildren()) do
		if stat.Value == true then
			playerStats[stat.Name] = true
		end
	end
	return playerStats
end

local function onPlayerExit(player)  
	if debounceTable[player.UserId] and debounceTable[player.UserId] > tick() - 5 then
		return
	end
	debounceTable[player.UserId] = tick()

	local playerStats = createTable(player)
	local success, err
	local attempts = 0
	repeat -- repeat until SetAsync succeeds
		attempts += 1
		success, err = pcall(function()
			playerData:SetAsync("Player_" .. player.UserId, playerStats) 
			print("Data Saved")
		end)
		if not success then
			warn("DataStore Error: " .. tostring(err))
			local delay = math.pow(2, attempts) -- exponential backoff
			wait(delay)
		end
	until success

	debounceTable[player.UserId] = nil
end

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		onPlayerExit(player)
	end
end)

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

The output show a warning " data sent too frequently "
and sometime when player experience lag or even either game shutdown and crashed , dataloss occur , can you find the other way to save data without losing old data ? willing to pay you 4k+

Is the error written when exiting the game or when saving via while true?

theres no error , its just a warning data being sent too frequently . I have never experienced an error . Maybe main reason is bindtoclose and playerremoving running at the same thing which lead up a warning . And IF player lost connection , SOMETIME their data lost

I mean what exactly causes this warning

I had no clue but sometime i do get this kind of warning
“DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.” Maybe bindtoclose and playeremoving run at the same time?

Hello there! There are limits to the DataStore service.
All limits and workarounds can be found here.

Try to make a bool or table value, if the data is already saved, then don’t save again

Eh i dont get it , i mean how?

I see , maybe thats the reason

Try to temporarily remove the while loops and tell me if there will be more such warnings

ok i tried to make a warning appear again but it failed , seems good to me
image

and right now im having trouble with dealership , i joined the game and i lost all of my cars

It seems to me that something is wrong with the very principle of conservation, hm
Attemp to print data when you use GetAsync and print playerStats

Whats wrong? is there something wrong on dealership or money ?

Can we go to Discord? My Discord: фанта#7912

1 Like

Thank you for helping me ! thank you thank you thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.