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

Why is this Initial Value Inverting?

Asked by
Despayr 505 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
I think that is why you are subtracting "Data.Experience.Value = (Data.Experience.Value - Data.ExperienceNeeded.Value)" brok4d 77 — 5y
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. Despayr 505 — 5y
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. Despayr 505 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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!

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 Despayr 505 — 5y
0
Np BlackOrange3343 2676 — 5y
Ad

Answer this question