# Is their something wrong with my math?

So im making a game with a stat system and i keep running across this error. When ever my level increases, my points go to half of that level. Ive tried reseting my stats and this still occurs. When ever i remove the value completely, taking away the stat point increase per level stops this entirely but… well i get no points for leveling.

https://gyazo.com/23dc0f3ff58b2cfb36ef2e4ec61ed4d2

2 Likes

Instead of showing a video, mind copying and pasting the code?

2 Likes

what is the extra level variable used for? Bit confused, is it one level ahead of your own level.

1 Like

Alright here

local MaxLevel = 5000

local Stats = player:WaitForChild(“Stats”)
local Exp = Stats:WaitForChild(“Exp”)
local Points = Stats:WaitForChild(“Points”)

``````local Level = Stats:WaitForChild("Level")

local ExtraLevel = Level.Value
local ExpNeed = ((ExtraLevel * 2)-1) * 20 --How much Exp is needed per level
if Level.Value < MaxLevel then
if Exp.Value >= ExpNeed then
Points.Value = Points.Value + 2
Level.Value = Level.Value + 1
Exp.Value = Exp.Value - ExpNeed

end
end
``````

end)

The extra level variable is basically my current level being stored in a seperate variable

Are they number values? It works for me with number values! Not integers tho/

Nope, all my stat systems use int values

Try using number values:/ it works with it

I swapped the int value with number value, it still does the same thing. Unless you put the whole thing in number values?

By this i mean i only made the Points value into a num value

where is ‘Max Level’ defined as? Also putting some prints may be helpful for debugging.

At the top, it says “Local Max level = 5000”

“local MaxLevel = 5000” my bad

Not too sure, but maybe check your number or int values while playing and seeing the value inside of it. You might get a better understanding of what is working, or changing wrongly, and you could work from there.

I’m not sure either, is there anymore code you can present?

Also:

``````local Level = Stats:WaitForChild("Level")

local ExpNeed = ((Level.Value * 2) - 1) * 20 --How much Exp is needed per level
if Level.Value < MaxLevel then
if Exp.Value >= ExpNeed then
Exp.Value = Exp.Value - ExpNeed -- I moved it to the top because the if
-- statement could be firing multiple times before it gets to Exp.Value - ExpNeed.
Points.Value = Points.Value + 2
Level.Value = Level.Value + 1
end
end
``````

Looks a little cleaner.

Thanks and i will:

This the other codes that can possibly influence this

local Points1 = DataStore:GetDataStore(“Points001”)

local stats = Instance.new(“Folder”, Plr)
stats.Name = “Stats”
— Level System
local Level = Instance.new(“NumberValue”, stats)
Level.Name = “Level”
Level.Value = 1

``````local Exp = Instance.new("NumberValue", stats)
Exp.Name = "Exp"
Exp.Value = 0

--- Money System
local Yen = Instance.new("IntValue", stats)
Yen.Name = "Yen"
Yen.Value = 0

--- Stats System
local Points = Instance.new("NumberValue", stats)
Points.Name = "Points"
Points.Value = 0

local MaxHealth = Instance.new("IntValue", stats)
MaxHealth.Name = "MaxHealth"
MaxHealth.Value = 100

local Stamina = Instance.new("IntValue", stats)
Stamina.Name = "Stamina"
Stamina.Value = 0

local Strength = Instance.new("IntValue", stats)
Strength.Name = "Strength"
Strength.Value = 0
``````

---- Datastore ----
— Levels
Level.Value = Level1:GetAsync(Plr.UserId) or Level.Value
Level1:SetAsync(Plr.UserId, Level.Value)
–Level.Changed:connect(function()
– Level1:SetAsync(Plr.UserId, Level.Value)
–end)
— Yen
Yen.Value = Yen1:GetAsync(Plr.UserId) or Yen.Value
Yen1:SetAsync(Plr.UserId, Yen.Value)
– Yen.Changed:connect(function()
– Yen1:SetAsync(Plr.UserId, Yen.Value)
– end)
— Exp
Exp.Value = Exp1:GetAsync(Plr.UserId) or Exp.Value
Exp1:SetAsync(Plr.UserId, Exp.Value)
– Exp.Changed:connect(function()
– Exp1:SetAsync(Plr.UserId, Exp.Value)
– end)
Strength.Value = Strength1:GetAsync(Plr.UserId) or Strength.Value
Strength1:SetAsync(Plr.UserId, Strength.Value)
– Strength.Changed:connect(function()
– Strength1:SetAsync(Plr.UserId, Strength.Value)
– end)
— Health
MaxHealth.Value = MaxHealth1:GetAsync(Plr.UserId) or MaxHealth.Value
MaxHealth1:SetAsync(Plr.UserId, MaxHealth.Value)
– MaxHealth.Changed:connect(function()
– MaxHealth1:SetAsync(Plr.UserId, MaxHealth.Value)
–end)
— Stamina
Stamina.Value = Stamina1:GetAsync(Plr.UserId) or Stamina.Value
Stamina1:SetAsync(Plr.UserId, Stamina.Value)
– Stamina.Changed:connect(function()
– Stamina1:SetAsync(Plr.UserId, Stamina.Value)
– end)
— Points
Points.Value = Points1:GetAsync(Plr.UserId) or Points.Value
Points1:SetAsync(Plr.UserId, Points.Value)
–Points.Changed:connect(function()
–Points1:SetAsync(Plr.UserId, Points.Value)
end)

game.Players.PlayerRemoving:connect(function(Player)
Level1:SetAsync(Player.UserId, Player.Stats.Level.Value)
Yen1:SetAsync(Player.UserId, Player.Stats.Yen.Value)
Exp1:SetAsync(Player.UserId, Player.Stats.Exp.Value)
Strength1:SetAsync(Player.UserId, Player.Stats.Strength.Value)
MaxHealth1:SetAsync(Player.UserId, Player.Stats.MaxHealth.Value)
Stamina1:SetAsync(Player.UserId, Player.Stats.Stamina.Value)
Points1:SetAsync(Player.UserId, Player.Stats.Points.Value)
end)

while wait(30) do
Level1:SetAsync(Player.UserId, Player.Stats.Level.Value)
Yen1:SetAsync(Player.UserId, Player.Stats.Yen.Value)
Exp1:SetAsync(Player.UserId, Player.Stats.Exp.Value)
Strength1:SetAsync(Player.UserId, Player.Stats.Strength.Value)
MaxHealth1:SetAsync(Player.UserId, Player.Stats.MaxHealth.Value)
Stamina1:SetAsync(Player.UserId, Player.Stats.Stamina.Value)
Points1:SetAsync(Player.UserId, Player.Stats.Points.Value)

``````end
``````

end)

also this:
script.Parent:WaitForChild(“Points”)

local Player = game.Players.LocalPlayer

local Parent = script.Parent

local Display = Parent:WaitForChild(“Number”)

local Stats = Player:WaitForChild(“Stats”)

local Points = Stats:WaitForChild(“Points”)

Display.Text = Points.Value

Points.Changed:Connect(function()

Display.Text = Points.Value

end)

EDIT: Found the solution. I was decreasing the points in a local script, meaning that on the server script it stayed the same. This made is so when ever i leveled up it will go to the value it was before, that being twice my level.

1 Like