Hello guys,
Recently I came across an issue while creating my level reward system.
When the player’s level reaches over the mission’s specific level, the frame will go over the intended stopping destination.
Here’s a video of it.
Code for it
local replicatedStorage = game:GetService("ReplicatedStorage")
local UpdateLevelUI = replicatedStorage:WaitForChild("UpdateLevelUI")
local debounce = false
UpdateLevelUI.OnServerEvent:Connect(function(player)
-- Player stats
local playerLevel = player:WaitForChild("Values").Level
local playerLevelFolder = player:WaitForChild("Missions")
-- Frame
local levelGUI = player.PlayerGui.Level
local rewardFrame = levelGUI.Level.Rewards
-- Find each mission
for _, v in ipairs(rewardFrame:GetChildren()) do
if not v:IsA("UIListLayout") then
for _, x in ipairs(v:GetChildren()) do
local progressBackground = x:FindFirstChild("ProgressBackground")
local progress = progressBackground:FindFirstChild("Progress")
local missionTitle = x:FindFirstChild("MissionTitle")
local titleScreen = x:FindFirstChild("TitleScreen")
titleScreen.Text = x:FindFirstChild("Title").Value
missionTitle.Text = "Be level "..x:FindFirstChild("Level").Value
progress:TweenSize(UDim2.new(playerLevel.Value / x:FindFirstChild("Level").Value, 0, 1, 0))
x:FindFirstChild("Claim").MouseButton1Down:Connect(function()
if not debounce then
debounce=true
if x:FindFirstChild("Level") then
if playerLevel.Value >= x:FindFirstChild("Level").Value then
if not playerLevelFolder:FindFirstChild("Level "..x:FindFirstChild("Level").Value).Value then
print("Level reward given")
playerLevelFolder:FindFirstChild("Level "..x:FindFirstChild("Level").Value).Value = true
else
warn("Already claimed!")
end
else
warn("Not enough level to claim this reward!")
end
end
task.wait(1)
debounce = false
end
end)
end
end
end
end)
You can use math.clamp! This limits a number into a specified minimum and maximum. For this case, we will limit playerLevel.Value / x:FindFirstChild("Level").Value (the number) into minimum of 0 and maximum of 1 (so it doesn’t overflow).
-- replace line 29 with the line below
progress:TweenSize(UDim2.fromScale(math.clamp(playerLevel.Value / x:FindFirstChild("Level").Value, 0, 1), 1))
Hello there,
Thank you for the suggestion. It is working as of now.
I have read on the Roblox creation page about it. Basically, Udim2.fromScale() has 2 parameters xScale and yScale. Normal UDim2.new() will take in 4 parameters. xScale, Xoffset, yScale and yOffset. As for the math.clamp part, it will take in 3 parameters. It will constraint the first number.
When set to true, portions of GuiObjects that fall outside of the BillboardGui’s canvas borders will not be drawn. Even when this property is false, objects that are completely outside of the canvas of the BillboardGui will not render.