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)