Hello Developers!
Hi and I am Kamikaze and I am new to scripting. I need help with saving data. When I try my script, the data does not save! Here is the script:
'''
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
local data
local sucess, errormessage = pcall(function()
data = DataStore:GetAsync(player.UserId..-wins)
end)
if sucess then
wins.Value = data
else
print("Error in getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local sucess, errormessage = pcall(function()
DataStore:SetAsync(player.UserId.."-wins",player.leaderstats.Wins.Value)
end)
if sucess then
print("Hooray! Data Saved!")
else
print("Oof! Your data was not saved!")
warn(errormessage)
end
end)
'''
Note that this script is in serverscriptservice
Also then error getting your data prints, but also the Hooray! Data saved! is also printed when you leave