Hey guys, another question.
I've added Exp to my game and while the ranks of players save, Exp does not. This means that if someone ranks up in my game, they will eventually lose everything because ranks are based on Exp and they would revert back to rank 1 due to having 0 Exp.
With it already working for the ranks, how would I implement Exp to also be saved? I've put a "-- Here" indicated where I've attempted to do this.
local DS = game:GetService('DataStoreService'):GetDataStore('Data')
local event = game.ReplicatedStorage.Event;
function onPlayerEntered(plr)
repeat wait() until plr.Character
local stats = Instance.new("Folder")
stats.Parent = plr
stats.Name = "leaderstats"
local rank = Instance.new("StringValue")
rank.Parent = stats
rank.Name = "Rank"
rank.Value = "Newbie"
local val = Instance.new("IntValue")
val.Parent = stats
val.Name = "RankVal"
val.Value = 1
local exp1 = Instance.new("IntValue")
exp1.Parent = stats
exp1.Name = "Exp"
exp1.Value = 0
local Key = plr.UserId
if DS:GetAsync(Key) == nil or DS:GetAsync(Key) == 0 then
DS:SetAsync(Key, 1)
end
plr.leaderstats.RankVal.Value = DS:GetAsync(Key)
plr.leaderstats.Exp.Value = DS:GetAsync(Key) -- Here
print("fired")
event:FireAllClients(plr.Name, 1);
-- attach the leaderstats changed event here
val.Changed:connect(function(value)
print("check'em");
event:FireAllClients(plr.Name, value);
end);
end
game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(function(plr)
local stats = plr.leaderstats
local Key = plr.UserID
DS:SetAsync(Key, stats.RankVal.Value)
DS:SetAsync(Key, stats.Exp.Value) -- Here
end)

6 comments
"UserID is not a valid member of Player"
which line?
its "UserId" not "UserID"
Actually that was a mistake on my part. There is no error for the output HOWEVER the script does not work. I tested it in a server and changed my rank manually, when I rejoined my Exp was 8, my rank was 8 and my class was Elder.
when u changed your rank, the game automatically saves it when u leave the game. All you have to do is to use developer console and change your rank back. Press F9 (or Fn + F9 for laptops) then go to Server Log. Then, type game.Players.YOURNAME.leaderstats.rank = YOURRANK
What rank did u change into? (To be honest) What rank do u wanna change into?