Help with save and load script

I’m trying to make a save load script. it seems to work but only sometimes. for example say I leave with 60 damage 70 kill and 50 coins and then rejoined I would have 60 damage 70 kill and 50 coins but sometimes I would leave with 60 damage 70 kill and 50 coins and when I rejoined it would be the save before that aka a completely random number here is my scrip.

--Sword Save---
local repstorage = game:GetService("ReplicatedStorage")

local Grassswordev = repstorage:WaitForChild("GrassSwordev")

--Sword Save---

local dataservice = game:GetService("DataStoreService")

local datastore = dataservice:GetDataStore("PlayerDataStore")

local swordstor = dataservice:GetDataStore("SwordDataStore")

local grassSword = nil

local basicsword = nil

local werewolfsword = nil

local swordsssss = {grassSword, basicsword, werewolfsword}

local function onPlayerJoin(player)
	wait(5)
	print(player)
	local UserId = player.UserId
	print(UserId)
	local data = datastore:GetAsync(UserId)
	print("getted")
	
	
	
	local coins = player.leaderstats.coins
	coins.Value = data[3]
	
	local damage = player.leaderstats.Damage
	damage.Value = data[2]
	
	local kills = player.leaderstats.Kills
	kills.Value = data[1]
	
	--Sword loading--	
	
	
	
end

local function onPlayerExit(player)
	local success, err = pcall(function()
	
	local monkeymoneys = {player.leaderstats.Kills.Value, player.leaderstats.Damage.Value, player.leaderstats.coins.Value, swordsssss}
		
		print(monkeymoneys[4][1])
		print(monkeymoneys[4][2])
		print(monkeymoneys[4][3])
		

	
		print(player)
		
	local playeruserid = player.UserId
	print(playeruserid)
	datastore:SetAsync(playeruserid, monkeymoneys)
		print("setted")	
end)

if success then
		
		return;
		
	else
		
		print("Uh oh there is an error saving data for " .. player.Name .. ", maybe ROBLOX datastores are down?")
	end
end

	
	

Grassswordev.OnServerEvent:Connect(function(player, sword)
	
	grassSword = 1
	
	basicsword = 1
	
	werewolfsword = 1
	
	if sword == "GrassSword" then
		grassSword = true
		print(grassSword)
	end
	
	swordsssss = {grassSword, basicsword, werewolfsword}
	

end)

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

The sword save on the end is just a bit of script i have not finished yet.

I suggest you wrap the code for the data saving and loading in pcalls. They can help you figure out if there are errors and also code outside of the pcalls can run safely. Pcall(), xpcall() and ypcall() Tutorial - How do they work? - #4 by BenMactavsin

Instead of using SetAsync(), use UpdateAsync(), which runs until the datastore is updated:

datastore:UpdateAsync(playeruserid, function()
	return monkeymoneys --the value is returned as the variable to set with
end)
print("setted")

I tried using updateASync but it did not work. there was no errors it just returned monkeymoneys as nil.

Just tried pcall and it seemed to work. didn’t even realize I did not have pcall around my load script lol