What do you want to achieve? I want to do a datasave of multiple values
What is the issue? It’s not saving the value
What solutions have you tried so far? I watch tutorials, i search on devforum, but i cannot find a datasave that is working for my game. Idk what is the issue (btw no errors in output and with another datsave it printed me “Data saved!”, but it didn’t work)
Here is the script (serverscriptservice, normalscript)
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue") --Sets up value for leaderstats
money.Name = "Points"
money.Parent = leaderstats
local exp = Instance.new("IntValue") --Sets up value for leaderstats
exp.Name = "Experience"
exp.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
money.Value = data['Money']
exp.Value = data['Experience']
else
-- Data store is working, but no current data for this player
money.Value = 0
exp.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
local datastoreservice = game:GetService("DataStoreService")
local mystore = datastoreservice:GetDataStore("mydatastore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local coins = Instance.new("IntValue")
coins.Name = "Points"
coins.Parent = leaderstats
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Parent = leaderstats
local playerid = "Player_" .. player.UserId
local data = mystore:GetAsync(playerid)
if data then
coins.Value = data['Coins']
gems.Value = data['Gems']
else
coins.Value = 0
gems.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local datatobesaved = {
Coins = player.leaderstats.Points.Value;
Gems = player.leaderstats.Gems.Value;
}
local playerid = "Player_" .. player.UserId
local success, err = pcall(function()
mystore:SetAsync(playerid,datatobesaved)
end)
if success then
print("data saved!")
else
print("data faield to save!")
end
end)
now i tried this datasave, but it is not working look
i play and my points are 0 (and it is normal)
i touch a part and my points now are 1
i leave and it prints me data saved
i join again and my points are 0
Ok. So after looking at this code and testing it. There’s nothing wrong at all. The game saved those values for me. May I see the points adding code? (where you add points)
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue") --Sets up value for leaderstats
money.Name = "Points"
money.Parent = leaderstats
local exp = Instance.new("IntValue") --Sets up value for leaderstats
exp.Name = "Experience"
exp.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
money.Value = data['Points']
exp.Value = data['Experience']
else
-- Data store is working, but no current data for this player
money.Value = 0
exp.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
lol, you thought the stat is named “money” but its actually “points”
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end