Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can anyone see why this leveling system is setting the level to the Max Experience?

This is supposed to add to the level once the current experience reaches the maximum experience needed. It seems to work for a while then starts setting the level to the Max Experience. I am sure this is something blatantly obvious and I am just not seeing it. Thanks.

local dataStore = require(game.ServerStorage.DataStore)
Players = game:GetService("Players")
player = Players.LocalPlayer
RS = game:GetService("ReplicatedStorage")
event = RS.Remotes:FindFirstChild("exp")

event.OnServerEvent:Connect(function(player)

local stats = player:WaitForChild("leaderstats")
local Level = stats.level
local current = stats.experience
local max = stats.Max

local gui = player.PlayerGui:FindFirstChild("Levels")
local ext = gui:WaitForChild("Exterior")
local lev = ext:WaitForChild("Label")
local exp = ext:WaitForChild("Exp")
local bar = ext:WaitForChild("Bar")

lev.Text = "Level "..Level.Value
exp.Text = current.Value.."/"..max.Value.." EXP"
bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)

	exp.Text = current.Value.."/"..max.Value.." EXP"
	bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)	
	    if current.Value >= max.Value then
			current.Value = 0
			max.Value = max.Value * 1.5
			Level.Value = Level.Value + 1
			dataStore:SetStat(player, "experience", 0)
			dataStore:SetStat(player, "Max", max.Value)
			exp.Text = tostring(0).."/"..max.Value.." EXP"
			bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)
		 end
		
	    Level.Changed:Connect(function() 
		     lev.Text = "Level "..Level.Value 
		     dataStore:AddStat(player, "level", 1) 
	    end)	
end)

1 answer

✓ Accepted answer

Why are you using :AddStat() with your datastore? You might want to just save this while the player is being removed and see if that helps.

3 comments

0

I always seem to have trouble with losing data, but I will try that and see if it helps. thanks.

0

It is still doing the same thing, but I did take out the AddStat(), because that is probably overkill on DataStores.

0

Well, because you made me go back and look at my DataStores, I found the problem, so I am going to accept your answer even if it wasn't what was wrong because I wouldn't have found it otherwise. Thanks! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.