Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to properly handle a player receiving experience in a game?

Asked by 8 years ago

I am currently making a leveling system for a small game I'm working on, and I made a system where when a user follows another player into the game, the player who was followed gets 350 experience, which counts towards their level. However, if they have almost all of the experience for the level they are on, and the 350 experience puts them onto the next level + some experience, I need to figure out the proper XP amount to show.

Basically: - User has 270/300 XP and they are on level 1 - User is followed, and as a reward they get 350 XP - They now technically have 620 experience, but instead they are put onto level 2 + [some amount of experience]

So, I need to figure out: A. How to properly find the amount of experience the user would have after getting more experience B. What the number [some amount of experience] would be.

If this is too complicated, sorry. I can elaborate.

2 answers

Log in to vote
1
Answered by 8 years ago

more complicated then it may seem. But simple enough to explain. It is simple math. So, lets say you have 245/300 XP but you just got rewarded 250 XP for some reason. This means you have 495/300. Thats not right. So, what you should do, is have a IntValue named "XP" and that holds all the experience. Next, name another IntValue, "LevelRequiredXP" and figure out a way so that changes as time progresses with the levels. So, now you have the XP, and you have the max XP for the required level. Perfect. Now, the harder part. You need to form a arithmatic operation to see if the value is less then or greater then the LevelRequiredXP value, and to make the remainder global throughout the player, not the world, either make this a local script, and make the remainder value global (add _G. before it) or add a value named "Remainder"

--other code(remember to announce all variables! That will be important!)
if (xp.Value + 250) > LevelRequiredXP.Value then
remainder = (xp.Value+250) - LevelRequiredXP.Value -- sets the amount needed
xp.Value = LevelRequiredXP.Value --next line optional, depending on the way you went for storage on the remainder value
REmainder.Value = ramainder
elseif (xp.Value + 250) <= LevelRequiredXP.Value then
xp.Value = xp.Value + 250
end
--any more code

So, now that you should have STUDIED THAT THOROUGHLY, set up a script handling leveling up, and checking that if the remainder value does not equal zero, it is added int the next rank immediately, then set back to zero. Study that well, and if you have questions feel free to contact me through ROBLOX as we do not have private messaging here,

0
Thank you for this. I will try to understand all of this by myself, even though I'm relatively new to scripting xD CoderLeo 50 — 8y
Ad
Log in to vote
0
Answered by
RoboFrog 400 Moderation Voter
8 years ago

This is all in theory and isn't guaranteed to work, but you could also do something like this --

Create a variable which is equal to the current max EXP for the level, then another which keeps count on the current experience. Within the script, have a comparison that checks if the current EXP has surpassed the max EXP variable once addition has finished, and when it does, have it subtract the current EXP by the old max EXP, and then multiply the max EXP variable by whatever number you use to calculate the next level's max EXP in order to ready it for the next level's subtraction while keeping a remainder.

If that doesn't make sense, I could attempt to rig a very prototype script detailing the above.

0
This doesn't really make sense. Can you post a prototype script? CoderLeo 50 — 8y

Answer this question