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

Am i doing this leveling script right with the leaderboard?

Ok, so, I want the required exp to go up every time the player levels up, but I only know how to script it on a constant up. Meaning you need a thousand or whatever I wanted to put exp to level up, and its constant. I want it to add lets say 250 exp required every level. Am I doing this right?

local expthing = 100

game.Players.PlayerAdded:connect(function(player)
	local folder = Instance.new("Folder")
	folder.Parent = player
	folder.Name = "leaderstats"
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = folder
	local Exp = Instance.new("IntValue")
	Exp.Name = "Experience"
	Exp.Value = 0
	Exp.Parent = folder
	local Gold = Instance.new("IntValue")
	Gold.Name = "Gold"
	Gold.Value = 0
	Gold.Parent = folder
	local Rubies = Instance.new("IntValue")
	Rubies.Name = "Rubies"
	Rubies.Value = 0
	Rubies.Parent = folder
	Exp.Changed:connect(function()
		while wait(2) do
	Level.Value = math.floor(Exp.Value/expthing)
	expthing  = expthing + 200
		
		
		end
		
	end)
end)

1 answer

✓ Accepted answer

The Changed for the intvalue passes the new value as a parameter you can use this to check if the player should level up

Example:-

-- just an example of how to simplify creating stats
local function mkStat(statType, name, par, val)
	local tmp = Instance.new(statType, par)
	tmp.Name = name
	if val then tmp.Value = val end
	return tmp
end

local addXpPerLevel = 250 --  the xp per level
game.Players.PlayerAdded:connect(function(plr)
	local folder = mkStat('Folder', 'leaderstats', plr)
	local level = mkStat('IntValue', 'Level', folder, 1)
	local exp = mkStat('IntValue', 'Experience', folder, 0)
	local gold = mkStat('IntValue', 'Gold', folder, 0)
	local rubies = mkStat('IntValue', 'Rubies', folder, 0)
	
	exp.Changed:connect(function(newVal) -- we have the new value as its arg
		local xpReq = level.Value * addXpPerLevel -- you could store this, it is not a good idea to calculate this each time the xp changes
		if newVal >= xpReq then
			exp.Value = newVal - xpReq -- we do not reset the xp we carry the value over
			level.Value = level.Value + 1
		end
	end)

end)

I hope this helps, please comment if you do not understand how / why this code works.

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