local Players = game:GetService("Players")
local RStorage = game:GetService("ReplicatedStorage")

local DataStore = game:GetService("DataStoreService")
local MainData = DataStore:GetDataStore("MainData")

local ListOfEvents = {["ExampleGiver"] = RStorage:WaitForChild("ExampleMoneyEvent"),
}

Players.PlayerAdded:Connect(function(plr)
	local playerKey = "id"..plr.UserId
	print("Created ".. playerKey)
	
	local stats = Instance.new("Folder", plr)
	stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", stats)
	cash.Name = "Cash"
	
	local LoadProgress = spawn(function()
		repeat wait(.1)
		local success, failure = pcall(function()
			local GetData = MainData:GetAsync(playerKey)
			if GetData then
				print("Data Loaded!")
				cash.Value = GetData
			else
				print("Created New Data!")
				cash.Value = 0
			end
		end)
		until success
	end)
	
	
	local EventResponse = spawn(function()
	ListOfEvents.ExampleGiver.OnServerEvent:Connect(function(player, num)
	if typeof(num) == "number" and num >= 0 then
		print("Safely Transferring Funds.")
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + num
		print(player.Name.. " currently has ".. cash.Value)
	else
		print"Transfer Invalid."
	end
	end)
	end)
	
	local SaveProgress = spawn(function()
		while wait(200) do
		MainData:SetAsync(playerKey, cash.Value)
		print("Progress Saved!", "Stats: ", cash.Value, "PlayerInfo: ".. playerKey)
		end
	end)
	
end)

21:27:39.206 - 502: API Services rejected request with error: HTTP 0 (HTTP 500 (HTTP/1.1 500 Internal Server Error)) 21:27:39.207 - Stack Begin 21:27:39.208 - Script 'ServerScriptService.ExampleConnection', Line 50 21:27:39.209 - Stack End

3 comments

0

I was testing this in the studio between two players, the first player's data saved while the second didn't.

0

Someone please help me, I thought I followed the instructions correctly.

0

Okay I think I solved it, pcall does the fix right? I just added that on the saving part, am I safe now?