user-interfaceluaroblox

Exp bar Gui not updating


I have set up a basic exp system with exp, levels, and a Gui. I have the gui set to update when joining, and when gaining exp or a level, but it will only update when joining even though I'm using the same function for all updates.

Where the code starts (Local script attached to a text button):

local expManager = require(game.ReplicatedStorage.ExpManager)

expManager.expGui.addExp.MouseButton1Click:Connect(function()
    
    expManager.addExp(25)
    
end)

Where that code leads (Module script within ReplicatedStorage, along with a RemoteEvent):

ExpManager.expEvent = game.ReplicatedStorage:WaitForChild("ExpEvent")

function ExpManager.addExp(expAmount)
    
    ExpManager.expEvent:FireServer("addExp", expAmount)
    
end

Where that leads (Server script within ServerScriptService that handles stat values and saving

local expEvent = game.ReplicatedStorage:WaitForChild("ExpEvent")

expEvent.OnServerEvent:Connect(function(plr, eventtype, amount)
    
    local character = plr.Character
    
    if eventtype == "addLevel" then
        plr.Stats.Level.Value += amount
        expEvent:FireClient(plr, "updateExp")
    end
    
    if eventtype == "addExp" then
        plr.Stats.expPoints.Value += amount
        expEvent:FireClient(plr, "updateExp")
    end
    
end)

Where that leads (Local script within StarterCharacterScripts):

local expManager = require(game.ReplicatedStorage.ExpManager)
local expEvent = game.ReplicatedStorage:WaitForChild("ExpEvent")

expEvent.OnClientEvent:Connect(function(eventtype)
    
    
    if eventtype == "updateExp" then
        expManager.updateExpBar()
    end
    
end)

Leading finally back to the same module script as earlier (I've left out several unrelated variables, but please let me know if seeing the whole script is necessary):

ExpManager.expGui = ExpManager.playerGui:WaitForChild("expGui")
ExpManager.expText = ExpManager.expGui.expBackground:WaitForChild("expText")
ExpManager.exp = ExpManager.player.Stats.expPoints.Value
ExpManager.level = ExpManager.player.Stats.Level.Value
ExpManager.expNeeded = math.round(100 * (ExpManager.level / 2))

function ExpManager.updateExpBar()
    
    ExpManager.expGui.expBackground.expBar:TweenSize(UDim2.new(ExpManager.exp / ExpManager.expNeeded, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.15, true)

    ExpManager.expText.Text = math.round(ExpManager.exp).. "/" .. math.round(ExpManager.expNeeded)

end

I've used print messages to tell if the code is getting stuck somewhere but no, even when gaining exp or a level the code makes it to the function that updates the Gui (which works properly when the player joins?) There are no error messages either.


Solution

  • Module script change that fixed the problem:

    function ExpManager.updateExpBar()
        
        ExpManager.exp = ExpManager.player.Stats.expPoints.Value
        ExpManager.level = ExpManager.player.Stats.Level.Value
        ExpManager.expNeeded = math.round(100 * (ExpManager.level / 2))
        
        ExpManager.expGui.expBackground.expBar:TweenSize(UDim2.new(ExpManager.exp / ExpManager.expNeeded, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.15, true)
        ExpManager.expText.Text = math.round(ExpManager.exp).. "/" .. math.round(ExpManager.expNeeded)
    
    end
    

    In this change I defined the expPoints value, level value, and expNeeded value within the function that updates the Exp bar GUI rather than outside of it. This seemed to fix the problem.