Datastore not working in studio

data store service isint working and i dont know how to fix it, someone told me its because of the server closes faster in studio, but cant seem to find a sulotion to it.

heres my script

local DataSS = game:GetService("DataStoreService")
local MyData = DataSS:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local data
	local data2
	local data3
	local data4
	local success, errormessage = pcall(function()
		data = MyData:GetAsync(player.UserId.."-SpawnL")
		data2 = MyData:GetAsync(player.UserId.."-Fruit")
		data3 = MyData:GetAsync(player.UserId.."-Sword")
		data4 = MyData:GetAsync(player.UserId.."-Melee")
	end)

	if success then
		player.SpawnL.Value = data
		player.Fruit.Value = data2
		player.Sword.Value = data3
		player.Melee.Value = data4
	else
		print("Error Getting Data")
		warn(errormessage)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local data
	local data2
	local data3
	local data4
	local data5
	local data6
	local success, errormessage = pcall(function()
		data = MyData:GetAsync(player.UserId.."-Level")
		data2 = MyData:GetAsync(player.UserId.."-Exp")
		data3 = MyData:GetAsync(player.UserId.."-SLevel")
		data4 = MyData:GetAsync(player.UserId.."-Exp2")
		data5 = MyData:GetAsync(player.UserId.."-FLevel")
		data6 = MyData:GetAsync(player.UserId.."-Exp3")
	end)
	
	if success then
		player.GLevel.Value = data
		player.GLevel.Current.Value = data2
		player.SLvl.Value = data3
		player.SLvl.Exp.Value = data4
		player.FLvl.Value = data5
		player.FLvl.Exp.Value = data6
	else
		print("Error Getting Data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		MyData:SetAsync(player.UserId.."-Level",player.GLevel.Value)
		MyData:SetAsync(player.UserId.."-Exp",player.GLevel.Current.Value)
		MyData:SetAsync(player.UserId.."-SLevel",player.SLvl.Value)
		MyData:SetAsync(player.UserId.."-Exp2",player.SLvl.Exp.Value)
		MyData:SetAsync(player.UserId.."-FLevel",player.FLvl.Value)
		MyData:SetAsync(player.UserId.."-Exp3",player.FLvl.Exp.Value)
		MyData:SetAsync(player.UserId.."-SpawnL",player.SpawnL.Value)
		MyData:SetAsync(player.UserId.."-Fruit",player.Fruit.Value)
		MyData:SetAsync(player.UserId.."-Sword",player.Sword.Value)
		MyData:SetAsync(player.UserId.."-Melee",player.Melee.Value)
	end)

	if success then
		print("Data Saved")
	else
		print("DataSave Falied")
		warn(errormessage)
	end
end)

game:BindToClose(function()
	wait(3)
end)

First of all, I would recommend saving everything into a single datastore as a table. Is there any errors in the logs?

there are no error, but how would i go as to make it into a single table?

MyData:SetAsync(player.UserId, {['level'] = lvl.Value, ['exp'] = exp.Value})

This may help you out DataStore (Saving & Loading Player Data) - Roblox Scripting Tutorial - YouTube

so something like this but everthing inside of it?

		MyData:SetAsync(player.UserId, {
			["-Level"] = player.GLevel.Value,
			["-Exp"] = player.GLevel.Current.Value,
			["-SLevel"] = player.SLvl.Value,
			["-Exp2"] = player.SLvl.Exp.Value
		})

Yes and use :UpdateASync() when saving datas

where would i type this? aaaaaaaaaaaaa

		MyData:SetAsync(player.UserId, {
			["Level"] = player.GLevel.Value,
			["Exp"] = player.GLevel.Current.Value,
			["SLevel"] = player.SLvl.Value,
			["Exp2"] = player.SLvl.Exp.Value,
			["FLevel"] = player.FLvl.Value,
			["Exp3"] = player.FLvl.Exp.Value,
			["SpawnL"] = player.SpawnL.Value,
			["Fruit"] = player.Fruit.Value,
			["Sword"] = player.Sword.Value,
			["Melee"] = player.Melee.Value
		}):UpdateASync()

here?, idk anymore im losing my brain cells

oh like this?

		MyData:UpdateAsync(player.UserId, {
			["Level"] = player.GLevel.Value,
			["Exp"] = player.GLevel.Current.Value,
			["SLevel"] = player.SLvl.Value,
			["Exp2"] = player.SLvl.Exp.Value,
			["FLevel"] = player.FLvl.Value,
			["Exp3"] = player.FLvl.Exp.Value,
			["SpawnL"] = player.SpawnL.Value,
			["Fruit"] = player.Fruit.Value,
			["Sword"] = player.Sword.Value,
			["Melee"] = player.Melee.Value
		})

Read the link i posted.

am i going to reapet for every data?

		local playerData = {
			["Level"] = player.GLevel.Value,
			["Exp"] = player.GLevel.Current.Value,
			["SLevel"] = player.SLvl.Value,
			["Exp2"] = player.SLvl.Exp.Value,
			["FLevel"] = player.FLvl.Value,
			["Exp3"] = player.FLvl.Exp.Value,
			["SpawnL"] = player.SpawnL.Value,
			["Fruit"] = player.Fruit.Value,
			["Sword"] = player.Sword.Value,
			["Melee"] = player.Melee.Value
		}
		MyData:UpdateAsync(player.UserId,function(oldValue)
			local pValue = oldValue or {DataId = 0}
			if playerData.SLevel == pValue.DataId then
				playerData.SLevel = playerData.SLevel + 1
				return playerData
			else
				return nil
			end
		end)

like this:

local playerData = {
			["DataId"] = 0,
			["Level"] = player.GLevel.Value,
			["Exp"] = player.GLevel.Current.Value,
			["SLevel"] = player.SLvl.Value,
			["Exp2"] = player.SLvl.Exp.Value,
			["FLevel"] = player.FLvl.Value,
			["Exp3"] = player.FLvl.Exp.Value,
			["SpawnL"] = player.SpawnL.Value,
			["Fruit"] = player.Fruit.Value,
			["Sword"] = player.Sword.Value,
			["Melee"] = player.Melee.Value
		}
		MyData:UpdateAsync(player.UserId,function(oldValue)
			local pValue = oldValue or {DataId = 0}
			if playerData.DataId == pValue.DataId then
				playerData.DataId = playerData.DataId + 1
				return playerData
			else
				return nil
			end
		end)

k, but now my question is how would i replace the old data with the new data?

Use GetASync(plr.UserId)
Also make sure even the DataId is saving.

thats everything? just the players userid?

yep and it will return as a table with variables

ok idk if this is right please correct me

game.Players.PlayerAdded:Connect(function(player)
	local data
	local success, errormessage = pcall(function()
	 	data = MyData:GetAsync(player.UserId)(player.Character)
	end)
	
	if success then
		player.UserId = data
	else
		print("Error Getting Data")
		warn(errormessage)
	end
end)

its wrong i tested it came a error instantly