only half the time in roblox studio does the game save my deaths and kills and print "data saved" when i leave. The other half of the time it doesn't print anything. please help.
local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local kills = Instance.new("IntValue",leaderstats)
kills.Name = "Kills"
local deaths = Instance.new("IntValue",leaderstats)
deaths.Name = "Deaths"
local dataSave
local success, errormsg = pcall(function()
dataSave = myDataStore:GetAsync(player.UserId)
end)
if success then
deaths.Value = dataSave.deaths
kills.Value = dataSave.kills
else
warn(errormsg)
print("Data not saved")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local oldDataSave = {
deaths = plr.leaderstats.Deaths.Value;
kills = plr.leaderstats.Kills.Value;
}
local success, errormsg = pcall(function()
myDataStore:SetAsync(plr.UserId,oldDataSave)
end)
if success then
print("Data saved")
elseif errormsg then
print("Data not saved")
warn(errormsg)
end
end)

1 comment
Edit as you like.