Hello, i was making a game and got a script that saves the time, i added lines that supposed to save money too but it doesnt, it seems like code issue, not datastore itself, here is the code.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
-- Function to save player's money to DataStore
local function savePlayerMoney(playerId, moneyValue)
local success, result = pcall(function()
DataStore:SetAsync("Money_"..playerId, moneyValue)
end)
if success then
print("Data saved for player with ID ".. playerId .. ", Money: ".. moneyValue)
else
warn("Failed to save data for player with ID ".. playerId .. ": ".. tostring(result))
end
end
-- Function to handle player added event
local function onPlayerAdded(player)
wait()
local leaderstats = Instance.new("Folder")
leaderstats.Name = "stats"
leaderstats.Parent = player
local minutes = Instance.new("IntValue")
minutes.Name = "time"
minutes.Parent = leaderstats
local moneuh = Instance.new("IntValue")
moneuh.Name = "money"
moneuh.Parent = leaderstats
moneuh.Value = 15000
local success, result = pcall(function()
return DataStore:GetAsync("Minutes_"..player.UserId) and DataStore:GetAsync("Money_"..player.UserId)
end)
if success then
if result then
minutes.Value = result
end
else
warn("Failed to load data for player ".. player.Name.. ": ".. tostring(result))
end
task.spawn(function()
while task.wait(1) do
minutes.Value += 1
end
end)
end
-- Function to handle player removing event
local function onPlayerRemoving(player)
local minutes = player:FindFirstChild("stats") and player.stats:FindFirstChild("time")
local money = player:FindFirstChild("stats") and player.stats:FindFirstChild("money")
if minutes then
local success, result = pcall(function()
DataStore:SetAsync("Minutes_"..player.UserId, minutes.Value)
end)
if success then
print("Data saved for player ".. player.Name)
else
warn("Failed to save data for player ".. player.Name.. ": ".. tostring(result))
end
end
if money then
local success, result = pcall(function()
DataStore:SetAsync("Money_"..player.UserId, money.Value)
end)
if success then
print("Data saved for player ".. player.Name)
else
warn("Failed to save data for player ".. player.Name.. ": ".. tostring(result))
end
end
end
-- Function to save player data on game closure
local function saveDataOnGameClose()
for _, player in pairs(Players:GetPlayers()) do
local minutes = player:FindFirstChild("stats") and player.stats:FindFirstChild("time")
if minutes then
local success, errorMessage = pcall(function()
DataStore:SetAsync("Minutes_"..player.UserId, minutes.Value)
end)
if success then
print("Data saved for player ".. player.Name)
else
print("Error while saving data for player ".. player.Name)
warn(errorMessage)
end
end
-- Save money value
local moneyValue = player:FindFirstChild("stats") and player.stats:FindFirstChild("money") -- Corrected lowercase "money"
if moneyValue then
savePlayerMoney(player.UserId, moneyValue.Value)
end
end
end
-- Connect events
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(saveDataOnGameClose)
-- Connect Save event to save player's money
--ReplicatedStorage.Save.OnServerEvent:Connect(function(player, moneyValue)
-- local playerId = player.UserId
-- savePlayerMoney(playerId, moneyValue)
--end)
Can someone tell me whats wrong here, help me out please?