So I have a script which saves player data whenever the player leaves. But I would want to make it so that it autosaves the data every few seconds. Because when a player leaves, I would be able to call on a function with the parameter of that specific player.
But if I were to make a loop, I will not be able to get each specific name of the players.
I have thought of making a 'LocalScript' which would be imported into every player when they join. And then I would be able to use game.Players.LocalPlayer to get the specific player. But I want to see if there are other methods of doing this.
My original script is here:
local DSService = game:GetService("DataStoreService"):GetDataStore("DataSaving832723579985723")
game.Players.PlayerAdded:connect(function(player)
local playerId = "ID-"..player.userId
local leaderstats = Instance.new("IntValue", player)
leaderstats.Name = "leaderstats"
local money = Instance.new("IntValue", leaderstats)
money.Name = "Money"
money.Value = 0
local exp = Instance.new("IntValue", leaderstats)
exp.Name = "Experience"
exp.Value = 0
local level = Instance.new("IntValue", leaderstats)
level.Name = "Level"
level.Value = 0
exp.Changed:connect(function()
level.Value = math.floor(exp.Value / 1000)
end)
local HasSaved = DSService:GetAsync(playerId)
if HasSaved then
money.Value = HasSaved[1]
exp.Value = HasSaved[2]
level.Value = HasSaved[3]
else
local toSave = {money.Value, exp.Value, level.Value}
DSService:SetAsync(playerId, toSave)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local playerId = "ID-"..player.userId
local Save = {player.leaderstats.Money.Value, player.leaderstats.Experience.Value, player.leaderstats.Level.Value}
DSService:SetAsync(playerId, Save)
end)
I hope someone can give me another idea for this.
