so I asked ChatGPT for the base script and i just need help modifying it
(both single player saving and multiple player saving dosent work)
local DataStoreService = game:GetService("DataStoreService")
local leaderstatName = "Money" -- Replace with your leaderstat name
local dataStore = DataStoreService:GetDataStore("SharedLeaderstats") -- Name of the DataStore
-- Function to synchronize leaderstat values among players
local function synchronizeLeaderstats(player)
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
local moneyStat = leaderstats:FindFirstChild(leaderstatName)
if not moneyStat then
moneyStat = Instance.new("NumberValue")
moneyStat.Name = leaderstatName
moneyStat.Value = 0 -- Set initial value
moneyStat.Parent = leaderstats
moneyStat.Changed:Connect(function(newValue)
if typeof(newValue) == "number" then
local key = "Money_" .. player.UserId -- Create a unique key for the player
dataStore:SetAsync(key, newValue)
end
end)
end
end
-- Function to save leaderstat value to DataStore
local function saveLeaderstat(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local moneyStat = leaderstats:FindFirstChild(leaderstatName)
if moneyStat then
local key = "Money_" .. player.UserId -- Create a unique key for the player
dataStore:SetAsync(key, moneyStat.Value)
end
end
end
-- Synchronize leaderstats when a player joins the game
game.Players.PlayerAdded:Connect(function(player)
synchronizeLeaderstats(player)
end)
-- Save leaderstats when a player leaves the game
game.Players.PlayerRemoving:Connect(function(player)
saveLeaderstat(player)
end)