Hello, I have a folder in rep storage with a list of numbervalues which i would like to save whenever a player leaves and load whenever a player joins aka saving there data ive tried a few things and they havent worked could anyone help me?
I tried this didnt work
-- Get the DataStore service
local player = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
-- Function to save player data when they leave
local function savePlayerData(player)
-- Create a table to store the player's data
local data = {
-- Add your data here, for example:
Money = script.Parent.Money.Value,
Bitcoin = script.Parent.Crypto_Values.Bitcoin.Value
}
-- Save the data to the DataStore
local success, errorMessage = pcall(function()
playerDataStore:SetAsync(player.UserId, data)
end)
if not success then
warn("Error saving player data: " .. errorMessage)
end
end
-- Function to load player data when they join
local function loadPlayerData(player)
-- Get the data from the DataStore
local data = playerDataStore:GetAsync(player.UserId)
-- If data exists, set the player's data to the loaded values
if data then
-- Update the player's data with the loaded values
script.Parent.Money.Value = data.Money
script.Parent.Crypto_Values.Bitcoin.Value = data.Bitcoin
end
end
-- Connect the functions to the PlayerAdded and PlayerRemoving events
loadPlayerData()
player.PlayerRemoving:Connect(savePlayerData)
This is what re[ storage looks like