I use datastore2 to store exp data for use in lobbies and matches.
When the match ends, it will update the exp data and teleport the player to the lobby.
In the code below, I have condensed some unnecessary functions, mainly describing how I handle the add data function:
local module = {}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)
DataStore2.Combine("DATA", "level")
function FormatLevelDefault()
return {
lv=1,
exp=0,
}
end
function module:GetLevelByPlayer(player : Player)
local lv = DataStore2("level", player)
return lv:Get(FormatLevelDefault())
end
function module:AddExpByPlayer(exp,player: Player)
local data=module:GetLevelByPlayer(player)
local temp=table.clone(data)
data.exp=exp+data.exp
local lv = DataStore2("level", player)
lv:Set(data)
local testdata=module:GetLevelByPlayer(player)
print("add exp x"..exp.." Total: "..testdata.exp)
return {
before=temp,
after=lv:Get(FormatLevelDefault())
}
end
return module
print("add exp x"..exp.." Total: "..testdata.exp)
This line correctly returns the increased value
But when teleported to the lobby, all of the saved matches data will MOSTLY be lost
When I tested in Studio, the data was still saved and increased normally (when setting the match it was a loop without teleporting it back to the lobby).
Hope to receive help from you.
Thanks.