First of all, you have 2 variables named weapon in your leaderstats creations, which leads to confusion,
Second: In the code where you store playerData in sessionData, you’re assigning the skill value directly to the playerData table but not considering the other data you might want to save, you might want to store more data so use a key-value structure like
playerData = {
Skill = skill.Value
}
You’re connecting a signal to skill:GetPropertyChangedSignal("Value"), but the connection might not properly update sessionData because the key might be incorrect:
sessionData[player.UserId].Skill = skill.Value
To save u a headache, i suggest u to take a deep dive into ProfileService, Saving data is quite easy, accessing it from the client can sometimes be a lil hard but for that there is something called ReplicaService.
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local database = dataStoreService:GetDataStore("data")
local sessionData = {}
players.PlayerAdded:Connect(function(player)
-- Create skill folder
local skillFolder = Instance.new("Folder")
skillFolder.Name = "Skills"
skillFolder.Parent = player
local skill = Instance.new("StringValue")
skill.Name = "Skill"
skill.Parent = skillFolder
-- Create weapon folder
local weaponFolder = Instance.new("Folder")
weaponFolder.Name = "Weapons"
weaponFolder.Parent = player
local weapon = Instance.new("StringValue")
weapon.Name = "Weapon"
weapon.Parent = weaponFolder
weapon.Value = ""
-- Create item folder
local itemFolder = Instance.new("Folder")
itemFolder.Name = "Items"
itemFolder.Parent = player
local item = Instance.new("StringValue")
item.Name = "Item"
item.Parent = itemFolder
item.Value = ""
-- Create skill collection folder
local skillCollection = Instance.new("Folder")
skillCollection.Name = "Skill Collection"
skillCollection.Parent = player
local skills = {"Fireball", "Punch", "Earth Spikes", "Blue Fireball", "Aquatic Tornado", "Hollow Purple"}
for _, skillName in ipairs(skills) do
local skillStatus = Instance.new("BoolValue")
skillStatus.Name = skillName .. " Collected Status"
skillStatus.Parent = skillCollection
skillStatus.Value = false
end
-- Create fighting check
local fighting = Instance.new("BoolValue")
fighting.Name = "playerFightingCheck"
fighting.Parent = player
fighting.Value = false
-- Load player data
local success, playerData
local attempt = 0
repeat
attempt += 1
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
if not success then
warn("Failed to load data: " .. playerData)
task.wait(3)
end
until success or attempt >= 5
if success then
print("Connected to database")
if not playerData then
print("Assigning default data")
playerData = {
Skill = skill.Value
}
else
-- Load the saved data into the player's Skill
skill.Value = playerData.Skill or ""
end
sessionData[player.UserId] = playerData
else
warn("Unable to get data for: " .. player.UserId)
player:Kick("Unable to load your data. Try again later!")
end
-- Listen for changes to the Skill value
skill:GetPropertyChangedSignal("Value"):Connect(function()
if sessionData[player.UserId] then
sessionData[player.UserId].Skill = skill.Value
end
end)
end)
players.PlayerRemoving:Connect(function(player)
if sessionData[player.UserId] then
local success, errorMessage
local attempt = 0
repeat
attempt += 1
success, errorMessage = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
end)
if not success then
warn("Failed to save data: " .. errorMessage)
task.wait(3)
end
until success or attempt >= 5
if success then
print("Data saved for " .. player.Name)
else
warn("Unable to save data for " .. player.Name)
end
-- Clean up session data
sessionData[player.UserId] = nil
end
end)
It didn’t work, I’m pressing the play button on roblox studio and ending the session by pressing the stop button. Also, I checked and the value does change in game, so thats not an issue.
May you run this script for testing purposes, to check if the data actually saves for u, if it doesnt it would be weird since it does for me
I implemented a coin stat that increments by the second
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local database = dataStoreService:GetDataStore("data")
local sessionData = {}
players.PlayerAdded:Connect(function(player)
-- Create leaderstats folder for leaderboard
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Create Coins value to show on leaderboard
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0
-- Create skill folder
local skillFolder = Instance.new("Folder")
skillFolder.Name = "Skills"
skillFolder.Parent = player
local skill = Instance.new("StringValue")
skill.Name = "Skill"
skill.Parent = skillFolder
-- Create weapon folder
local weaponFolder = Instance.new("Folder")
weaponFolder.Name = "Weapons"
weaponFolder.Parent = player
local weapon = Instance.new("StringValue")
weapon.Name = "Weapon"
weapon.Parent = weaponFolder
weapon.Value = ""
-- Create item folder
local itemFolder = Instance.new("Folder")
itemFolder.Name = "Items"
itemFolder.Parent = player
local item = Instance.new("StringValue")
item.Name = "Item"
item.Parent = itemFolder
item.Value = ""
-- Create skill collection folder
local skillCollection = Instance.new("Folder")
skillCollection.Name = "Skill Collection"
skillCollection.Parent = player
local skills = {"Fireball", "Punch", "Earth Spikes", "Blue Fireball", "Aquatic Tornado", "Hollow Purple"}
for _, skillName in ipairs(skills) do
local skillStatus = Instance.new("BoolValue")
skillStatus.Name = skillName .. " Collected Status"
skillStatus.Parent = skillCollection
skillStatus.Value = false
end
-- Create fighting check
local fighting = Instance.new("BoolValue")
fighting.Name = "playerFightingCheck"
fighting.Parent = player
fighting.Value = false
-- Load player data
local success, playerData
local attempt = 0
repeat
attempt += 1
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
if not success then
warn("Failed to load data: " .. playerData)
task.wait(3)
end
until success or attempt >= 5
if success then
print("Connected to database")
if not playerData then
print("Assigning default data")
playerData = {
Skill = skill.Value,
Coins = 0 -- Initialize coins if no data exists
}
else
-- Load the saved data into the player's Skill and Coins
skill.Value = playerData.Skill or ""
coins.Value = playerData.Coins or 0
end
sessionData[player.UserId] = playerData
else
warn("Unable to get data for: " .. player.UserId)
player:Kick("Unable to load your data. Try again later!")
end
-- Increment coins by 1 every second for testing purposes
while player.Parent do
coins.Value += 1
if sessionData[player.UserId] then
sessionData[player.UserId].Coins = coins.Value
end
task.wait(1) -- Wait for 1 second
end
-- Listen for changes to the Skill value
skill:GetPropertyChangedSignal("Value"):Connect(function()
if sessionData[player.UserId] then
sessionData[player.UserId].Skill = skill.Value
end
end)
end)
players.PlayerRemoving:Connect(function(player)
if sessionData[player.UserId] then
local success, errorMessage
local attempt = 0
repeat
attempt += 1
success, errorMessage = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
end)
if not success then
warn("Failed to save data: " .. errorMessage)
task.wait(3)
end
until success or attempt >= 5
if success then
print("Data saved for " .. player.Name)
else
warn("Unable to save data for " .. player.Name)
end
-- Clean up session data
sessionData[player.UserId] = nil
end
end)
game:BindToClose(function()
for i, player in ipairs(game.Players:GetPlayers()) do
if sessionData[player.UserId] then
local success, errorMessage
local attempt = 0
repeat
attempt += 1
success, errorMessage = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
end)
if not success then
warn("Failed to save data: " .. errorMessage)
task.wait(3)
end
until success or attempt >= 5
if success then
print("Data saved for " .. player.Name)
else
warn("Unable to save data for " .. player.Name)
end
-- Clean up session data
sessionData[player.UserId] = nil
end
end
end)
I’ll try this out, thanks! I also heard a few minutes ago that I should parent objects after I declare Async or something so that the script doesn’t get confused. Is this true?