Greetings developers!
I’ve been having issues when making a levelling system w/ datastores. Seems to be working fine, but every time i rejoin the game i automatically get an extra level for no reason. How could i avoid this?
Code:
Players.PlayerAdded:Connect(function(player)
local Folder = Instance.new("Folder", player)
Folder.Name = "Level"
local Level = Instance.new("NumberValue", Folder)
Level.Name = "Level" -- yeah Level.Level
Level.Value = 1
local Exp = Instance.new("NumberValue", Folder)
Exp.Name = "Exp"
Exp.Value = 0
local NeededExp = Instance.new("NumberValue", Folder)
NeededExp.Name = "NeededExp"
if Level.Value == 0 then
NeededExp.Value = 50
else
NeededExp.Value = Level.Value * 50
end
Exp.Changed:Connect(function()
if Exp.Value >= NeededExp.Value then
Exp.Value = 0
Level.Value += 1
NeededExp.Value = Level.Value * 50
end
end)
Are you using DatastoreSerive to save your progress when you leave or is this code directly copy and pasted and doesn’t use DatastoreService? If not, I don’t really see a problem here
Here’s the full script, i thought what i showed would be enough to fix the problem:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local LevelsStore = DataStoreService:GetDataStore("LevelsStoring")
Players.PlayerAdded:Connect(function(player)
--Setting values up
local Folder = Instance.new("Folder", player)
Folder.Name = "Level"
local Level = Instance.new("NumberValue", Folder)
Level.Name = "Level"
Level.Value = 1
local Exp = Instance.new("NumberValue", Folder)
Exp.Name = "Exp"
Exp.Value = 0
local NeededExp = Instance.new("NumberValue", Folder)
NeededExp.Name = "NeededExp"
if Level.Value == 0 then
NeededExp.Value = 50
else
NeededExp.Value = Level.Value * 50
end
Exp.Changed:Connect(function(NewExp)
if Exp.Value >= NeededExp.Value then
Exp.Value -= NeededExp.Value
Level.Value += 1
NeededExp.Value = Level.Value * 50
end
end)
local ddatalevels
local expdata
local neededexpdata
local success, errormessage = pcall(function()
ddatalevels = LevelsStore:GetAsync(player.UserId.."-Level")
expdata = LevelsStore:GetAsync(player.UserId.."-Exp")
neededexpdata = LevelsStore:GetAsync(player.UserId.."-NeededExp")
end)
if success then
Level.Value = ddatalevels
Exp.Value = expdata
NeededExp.Value = neededexpdata
else
warn(errormessage)
end
end)
Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
local leveldata = LevelsStore:SetAsync(player.UserId.."-Level", player.Level.Level.Value)
local Expdata = LevelsStore:SetAsync(player.UserId.."-Exp", player.Level.Exp.Value)
local NeededExpdata = LevelsStore:SetAsync(player.UserId.."-NeededExp", player.Level.NeededExp.Value)
if success then
print("Data was saved!")
else
warn("Hello world! "..errormessage)
end
end)
game:BindToClose(function()
for i, playepplrs in pairs(Players:GetPlayers()) do
playepplrs:Kick("Server has been shutted down.")
end
wait(2)
end)
Sorry if it looks messy, it’s my first time using datastores
When you are getting an extra lvl is the level 2 or 1 after you joined? Im sorry if the question is a bit dumb but it helps me to understand your problem