I want to switch to UpdateAsync so players dont keep losing their data, but im not sure how to edit my code for it please help.
local Data = game:GetService('DataStoreService'):GetDataStore('rdata2')
local module = require(game.ServerScriptService.module)
local prefix = 'user_' -- example prefix
local rep = game:GetService("ReplicatedStorage")
local e = rep.events.save
game.Players.PlayerAdded:connect(function(player)
local data = Instance.new('Configuration')
data.Name = "Data"
data.Parent = player
local strength = Instance.new('NumberValue')
strength.Name = "strength"
strength.Parent = data
local strengthxp = Instance.new('NumberValue')
strengthxp.Name = "strengthxp"
strengthxp.Parent = data
local ability = Instance.new('StringValue')
ability.Name = "ability"
ability.Parent = data
local style = Instance.new('StringValue')
style.Name = "style"
style.Parent = data
local stamina = Instance.new('NumberValue')
stamina.Name = "stamina"
stamina.Parent = data
local staminaxp = Instance.new('NumberValue')
staminaxp.Name = "staminaxp"
staminaxp.Parent = data
local defense = Instance.new('NumberValue')
defense.Name = "defense"
defense.Parent = data
local defensexp = Instance.new('NumberValue')
defensexp.Name = "defensexp"
defensexp.Parent = data
local cash = Instance.new('NumberValue')
cash.Name = "cash"
cash.Parent = data
local race = Instance.new('StringValue')
race.Name = "race"
race.Parent = data
local nowstamina = Instance.new('NumberValue')
nowstamina.Name = "nowstamina"
nowstamina.Parent = data
local doingquest = Instance.new('BoolValue')
doingquest.Name = "doingquest"
doingquest.Parent = data
local CanRun = Instance.new('BoolValue')
CanRun.Name = "CanRun"
CanRun.Parent = data
CanRun.Value = true
local CanUseSkill = Instance.new('BoolValue')
CanUseSkill.Name = "CanUseSkill"
CanUseSkill.Parent = data
CanUseSkill.Value = true
local ate = Instance.new('NumberValue')
ate.Name = "ate"
ate.Parent = data
local killed = Instance.new('NumberValue')
killed.Name = "killed"
killed.Parent = data
local pData = Data:GetAsync(prefix .. tostring(player.UserId))
if pData then
strength.Value = pData[1] or 1
strengthxp.Value = pData[2] or 0
race.Value = pData[3] or "human"
defense.Value = pData[4]or 1
defensexp.Value = pData[5]or 0
ability.Value = pData[6]or ""
style.Value = pData[7]or ""
stamina.Value = pData[8]or 1
staminaxp.Value = pData[9]or 0
ate.Value = pData[10]or 0
cash.Value = pData[11]or 0
killed.Value = pData[12]or 0
else
strength.Value = 1
strengthxp.Value = 0
race.Value = "human"
defense.Value = 1
defensexp.Value = 0
ability.Value = ""
style.Value = ""
stamina.Value = 1
staminaxp.Value = 0
ate.Value = 0
cash.Value = 0
killed.Value = 0
end
coroutine.resume(coroutine.create(function()
while wait(120) do
Data:SetAsync(prefix .. tostring(player.UserId), {
strength.Value,
strengthxp.Value,
race.Value,
defense.Value,
defensexp.Value,
ability.Value,
style.Value,
stamina.Value,
staminaxp.Value,
ate.Value,
killed.Value,
})
end
end))
e.OnServerInvoke = function(Player)
local d = Player.Data
local strength = d.strength
local strengthxp = d.strengthxp
local defense = d.defense
local defensexp = d.defensexp
local ability = d.ability
local style = d.style
local race = d.race
local ate = d.ate
local killed = d.killed
Data:SetAsync(prefix .. tostring(Player.UserId), {
strength.Value,
strengthxp.Value,
race.Value,
defense.Value,
defensexp.Value,
ability.Value,
style.Value,
stamina.Value,
staminaxp.Value,
ate.Value,
killed.Value,
})
return "saved"
end
game.Players.PlayerRemoving:connect(function(Player)
local d = Player.Data
local strength = d.strength
local strengthxp = d.strengthxp
local defense = d.defense
local defensexp = d.defensexp
local ability = d.ability
local style = d.style
local race = d.race
local ate = d.ate
local killed = d.killed
Data:SetAsync(prefix .. tostring(Player.UserId), {
strength.Value,
strengthxp.Value,
race.Value,
defense.Value,
defensexp.Value,
ability.Value,
style.Value,
stamina.Value,
staminaxp.Value,
ate.Value,
killed.Value,
})
end)
---------------------------------------------------------------------------------------------------------------------------------
end)
