I was making a game, with a status system that includes some points to give away. I used roblox's DataStore service, but I'm having a problem with my game. After a player spends his points, distributing them in the stats he wants, after he leaves the game and re-enters those same points back to him with the same value, this can cause a point doubling glitch, which makes the unfair game, which I don't want. I think there is an error in my code, what can I do to fix it?
Code below:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlrData")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(Plr)
local data = Instance.new("Folder", Plr)
data.Name = "Data"
local exp = Instance.new("IntValue", data)
exp.Name = "Exp"
exp.Value = 1
local levels = Instance.new("IntValue", data)
levels.Name = "Level"
levels.Value = 1
local Points = Instance.new("IntValue", data)
Points.Name = "Points"
Points.Value = 3
local expneed = Instance.new("IntValue", data)
expneed.Name = "ExpNeeded"
expneed.Value = 100
local strenght = Instance.new("IntValue", data)
strenght.Value = 1
strenght.Name = "Strenght"
local Health = Instance.new("IntValue", data)
Health.Value = 100
Health.Name = "Health"
local char = Plr.Character or Plr.CharacterAdded:Wait()
local charhealth = char.Humanoid.Health
local DF = Instance.new("IntValue", data)
DF.Value = 1
DF.Name = "DF"
local PlayerId = Plr.UserId
local PlayerData = DataStore:GetAsync(PlayerId)
if PlayerData then
strenght.Value = PlayerData["Strenght"]
Health.Value = PlayerData["Health"]
DF.Value = PlayerData["DF"]
exp.Value = PlayerData["Exp"]
levels.Value = PlayerData["Level"]
expneed.Value = PlayerData["ExpNeeded"]
Points.Value = PlayerData["Points"]
charhealth = PlayerData["Health"]
end
end)
local function tab(Player)
local PlrStats = {}
for _, v in pairs(Player.Data:GetChildren()) do
PlrStats[v.Name] = v.Value
end
return PlrStats
end
game.Players.PlayerRemoving:Connect(function(Plr)
local PlrStats = tab(Plr)
local Sucess, Result = pcall(function()
local PlrId = Plr.UserId
DataStore:SetAsync(PlrId, PlrStats)
end)
if not Sucess then
print(Result)
warn("Error save")
end
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
task.wait()
local PlrStats = tab(Player)
local Sucess, Result = pcall(function()
local PlrId = Player.UserId
DataStore:SetAsync(PlrId, PlrStats)
end)
if not Sucess then
print(Result)
warn("Error")
end
end
end)
game.Players.PlayerAdded:Connect(function(Player)
task.wait(0.1)
local Level = Player.Data.Level
local Exp = Player.Data.Exp
local ExpNeed = Player.Data.ExpNeeded
local Points = Player.Data.Points
local Strenght = Player.Data.Strenght
local Health = Player.Data.Health
local DF = Player.Data:WaitForChild("DF")
RunService.Heartbeat:Connect(function()
if Exp.Value == 0 and ExpNeed.Value == 0 then
Exp.Value = 1
ExpNeed.Value = 100
end
if Points.Value > 300 then
Points.Value = 300
end
if Exp.Value >= ExpNeed.Value then
Exp.Value = Exp.Value - ExpNeed.Value
Level.Value += 1
ExpNeed.Value *= 1.13
Points.Value += 3
end
if Strenght.Value == 0 or DF.Value == 0 or Health.Value == 0 then
Strenght.Value = 1
DF.Value = 1
Health.Value = 1
end
if Strenght.Value > 100 or DF.Value > 100 then
Strenght.Value = 100
DF.Value = 100
end
if Health.Value > 1000 then
Health.Value = 1000
end
end)
RunService.Heartbeat:Connect(function()
if Level.Value >= 100 then
Level.Value = 100
end
end)
end)