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

Why is this Initial Value Inverting?

Ok so when the experience is greater than or equal to the needed experience, it is suppose to increase the players level by 1, and subtract the needed experience to level up from the experience. In other words, say the experience that is needed to level up is 2450, if the player had gained 2452 experience, this should result in a remainder of 2 experience points. What it does instead is go to -2448.

ServerScript

RemoteEvents.LevelCap.OnServerEvent:Connect(function(Player, Stat)
	
	local Data = Player.Persistance:WaitForChild("Data")
	
	if Data.Experience.Value >= Data.ExperienceNeeded.Value then
		wait(0.5)
		Data.Experience.Value = (Data.Experience.Value - Data.ExperienceNeeded.Value)
		wait(0.5)
		Data.ExperienceNeeded.Value = (Data.Level.Value * 50)
		Data.Level.Value = Data.Level.Value + 1
	end	
	
end)

LocalScript

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character ~= nil
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents
local Data = Player.Persistance:WaitForChild("Data")

local pcall function StatChange()

	RemoteEvents.LevelCap:FireServer("Experience")
	print("Hello")
end
Data.Experience.Changed:Connect(StatChange)

it does level up the player, but the experience just inverts.

3 comments

0

I think that is why you are subtracting "Data.Experience.Value = (Data.Experience.Value - Data.ExperienceNeeded.Value)"

0

Turns out that 2452 - 2450 = 2. Subtract 2450 again and you get 2448. It seems my problem is not that it is inverting, but because it is running that line twice.

0

The needed value is 2450. The experience is at 2452 so that would mean a level up with 2 remainder exp. But it seems that line 7 is running twice.

1 answer

✓ Accepted answer

Hi, I'm BlackOrange and I will be helping you out today!

Alright, so what I would do is make a function that checks if the EXP is more then or less then and then add the rest of the exp.

So I suggest making a level up function:

local function LevelUp()
	-- change current exp to 0
	-- update max exp needed to level up
	-- Update player level (add one)
end

local function CheckRemainderExp()
	if EXPGained > ExpNeededToLevelUp.Value then
		local Difference = EXPGained - EXPNeededToLevelUp.Value
		LevelUp()
		CurrentExp.Value = CurrentExp.Value + Difference -- this will add remainder after level up
	end
end

You need to update the variables

This is only a template. So you reset everything and then add remainder. This is a efficient way to do this so you don't need to check values. Of course you will need more conditional statements to make this better. Again this is only a template

Hopefully this template helped.

Best of luck developer!

2 comments

0

Thanks. I just made the script check twice and it works now. I will keep yours in mind as it is a much more efficient way. Thanks

0

Np

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