You’re using data stores incorrectly, you don’t save right after you loaded the data.
@JarodOfOrbiter was trying to say something like this:
To set up
local playerData = {}
playerData.weapons = weaponsData -- set these as the appropriate value, don't copy and paste
playerData.equipped = equippedData
playerData.player = player_data
playerData.wins = Winsdata
To load
local success, data = pcall(dataStores.GetAsync, "key", "value")
-- replace "key" and "value" with the data store's keysand value
You don’t use “function” here because the function is DataStore.GetAsync
/Data.SetAsync
. You only use function when you’re doing something like this:
local success, err = pcall(function()
return nil
end)
It just creates an anonymous function.
To save
local success, message = pcall(dataStores.SetAsync, "key", "value")
-- like the previous one, replace "key" and "value" with the key and value
Your end script should look something like this:
-- services to use
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStores = dataStoreService:GetDataStore("data store name here")
-- functions to get/save player data
local function SavePlayerData(player: Player)
local playerData = {}
playerData.weapons = -- the player's weapons
playerData.equipped = -- the equipped weapons the player has
playerData.player = {} -- the player's data here (coins etc): example: playerData.player = { money = 100, cash = 50 }
playerData.wins = -- the number of wins the player has
local success, message = pcall(dataStores.SetAsync, player.UserId, playerData) -- the playerdata to the player's userid data store
if not success then -- checks if it was able to successfully save the data
warn("unable to save data: " .. message)
end
end
local function GetPlayerData(player: Player)
local success, result = pcall(dataStores.GetAsync, player.UserId)
if not success then -- like save async, this checks if getting data was successful
warn("unable to load data: " .. result)
else
if result ~= nil then
-- do stuff with the player's data
end
end
end
-- connect the functions to the events
players.PlayerAdded:Connect(GetPlayerData)
players.PlayerRemoving:Connect(SavePlayerData)
-- incase something happens while the game is shutting down
game:BindToClose(function()
for _, plr in pairs(players:GetPlayers()) do -- loop through the players
coroutine.wrap(function() -- this is so the thread doesn't yield
SavePlayerData(plr) -- save their data
end)()
end
end)