Problem with :SetAsync()

Hello, I have a problem.
I execute the function below when a player leaves the game. But every other time when I look at my console the server stops at list finished and does not continue (so my game is not saved) and once on two the code goes all the way I have the message and these save do you have a solution?

function SaveData(player)
	if player:GetAttribute("SpamData") == nil then
		player:SetAttribute("SpamData", true)
		local BaseInfo = HttpService:JSONDecode(player.PlayerInfo.BaseInfo.Value)
		
		for x = 1, 9 do
			BaseInfo["Shortcut"..x] = player.PlayerInfo.Shortcut["Shortcut"..x].Value
		end
		BaseInfo.GoalID = player.PlayerInfo.GoalID.Value
		BaseInfo.LighterType = player.PlayerInfo.LighterType.Value
		if player.PlayerInfo:FindFirstChild("SaveNew") then
			print("Saving owner...")
			BaseInfo.SaveList[game.ReplicatedStorage.GameInfo.ServerSettings.ServerID.Value] = HttpService:JSONDecode(player.PlayerInfo.SaveNew.Value)
		end
		
		print("Saving client...")
		
		local LocalData = {}
		if BaseInfo.LocalData ~= nil then
			LocalData = BaseInfo.LocalData
		end
		
		local MyNewData = {}
		MyNewData.PlayerLocation = tostring(player:GetAttribute("LastPos"))
		
		if LocalData[game.ReplicatedStorage.GameInfo.ServerSettings.Owner.Value] == nil then
			LocalData[game.ReplicatedStorage.GameInfo.ServerSettings.Owner.Value] = {}
		end
		
		LocalData[game.ReplicatedStorage.GameInfo.ServerSettings.Owner.Value][game.ReplicatedStorage.GameInfo.ServerSettings.ServerID.Value] = MyNewData
		
		BaseInfo.LocalData = LocalData
		print("List finished")
		print(BaseInfo)
		
		local success, errorMassage = pcall(function()
			DataTable:SetAsync(player.UserId.."Data", BaseInfo)
		end)

		if success then
			print("The points has saved")
		else
			print("The points hasn't saved")
			warn(errorMassage)
		end
		
		
		print("Data saved with successful")
	end
end```

I found a few errors and I tried fixing them:

function SaveData(player)
if player:GetAttribute(“SpamData”) == nil then
player:SetAttribute(“SpamData”, true)

    local BaseInfo = HttpService:JSONDecode(player.PlayerInfo.BaseInfo.Value)

    for x = 1, 9 do
        BaseInfo["Shortcut" .. x] = player.PlayerInfo.Shortcut["Shortcut" .. x].Value
    end
    BaseInfo.GoalID = player.PlayerInfo.GoalID.Value
    BaseInfo.LighterType = player.PlayerInfo.LighterType.Value

    local LocalData = BaseInfo.LocalData or {}
    local MyNewData = {
        PlayerLocation = tostring(player:GetAttribute("LastPos"))
    }

    if LocalData[game.ReplicatedStorage.GameInfo.ServerSettings.Owner.Value] == nil then
        LocalData[game.ReplicatedStorage.GameInfo.ServerSettings.Owner.Value] = {}
    end

    LocalData[game.ReplicatedStorage.GameInfo.ServerSettings.Owner.Value][game.ReplicatedStorage.GameInfo.ServerSettings.ServerID.Value] = MyNewData

    BaseInfo.LocalData = LocalData

    print("List finished")
    print(BaseInfo)

    local success, errorMessage = pcall(function()
        DataTable:SetAsync(player.UserId .. "Data", BaseInfo)
    end)

    if success then
        print("The data has been saved")
    else
        warn("Error saving data:", errorMessage)
    end

    print("Data saved successfully")
end

end

And I added print statements which u can remove if ya want if this code works otherwise you might be able to spot the problem

1 Like

I just tried, I still have the problem :confused:

Do you mind sharing what’d it stand in the output?

image_2023-10-25_173902060

Did you get any errors / warnings?

1 Like

What I put to you these last lines that I have, no error nothing.

1 Like

Then the problem is soemthing with your game and not the code, double check that accessing the things works, the value and everything because no errors and it printed saved so the problem is something with your game. sometimes when I stumble across problems it’s not always the code, it’s my mechanics in the game so yeah I’d double check everything.

By carrying out tests, I noticed that by waiting a few minutes between each session the data is saved correctly.

Chatgpt :man_facepalming:

I dont know if anyone else has realized that youre using this, but you need to stop. Scripting SUPPORT is for help from experienced coders. Im not trying to just be a jerk, so ill list 2 reasons why you should stop using OpenAI.

  1. It gives deprecated code. Someone pointed out on one of your newer posts that wait() is deprecated. ChatGPT also tends to still use stuff such as Velocity.
  2. Its just not reliable. ChatGPT cant “playtest” code, so it will never know if something is wrong.

If you were really a beginner, you wouldnt know all of this. Another point is that LITERALLY nobody says “asynchronous behavior”

Bonus: you cant just act like you wrote everything. Thats called plagiarism.

Maybe im wrong about everything though. But I have seen ChatGPT in action before and their format is practically identical to yours.

4 Likes

Yeah I noticed her using chatGPT in the other scripting supports posts. Thanks for bringing this up.

1 Like

Finally I still have the problem, I don’t understand

I found the solution, I also use :BindToClose which solved the problem.

1 Like

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