I need help to make a stats system like the system of "A one piece game"

  1. What do you want to achieve? I want to make a stats system like the system of “A one piece game”
    the stats system of “A one piece game” image

  2. What is the issue? i dont know how to create that using just a For( or for and a function)

Scripts above
image
Structure of the bar
image
Player stats
image

  1. What solutions have you tried so far? youtube tutorial
-- --Set Bar

local StatsLabelFolder = StatsBackGround.StatsLabel:GetDescendants()


for i, v in pairs(StatsLabelFolder) do
	if v:IsA("Frame") then
		--Starting setting the bar size based on the stats value
	
	end
end

i need help, cuz i want to create that using a “for”

but i think, i can create a script for each bar for set the size based on stats value
Note: that is my first post on dev forum

1 Like

Since your stats are saved as physical values you can use .Changed to listen for a change in the stats and then update the GUI.

In your local script within the GUI:

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local playerStats = player:WaitForChild("PlayerStats"):GetChildren()

-- Creating a reference of all the stat names
local stats = {
	"Defense",
	"Strength",
	"Stamina",
	"Sword"
}

local StatsLabelFolder = StatsBackGround.StatsLabel:GetChildren()

-- Loop through all current stats
for _, stat in pairs(stats) do
	-- Find the frame that matches the current stat
	for _, statText in pairs(StatsLabelFolder) do
		if statText:IsA("TextLabel") then
			-- You can remove the "Label" at the end of your text
			-- labels to keep things matching or concatenate the "Label" 
			-- to the end of the stat name string
			if statText.Name == stat then -- Change "StrengthLabel" to "Strength"
				-- Listen for a change in the stat and update the
				-- correct frame
				playerStats[stat].Changed:Connect(function(newValue)
					-- I would keep everything within the text labels named
					-- the same thing to simplify the code
					local bar = statText.Bar -- Change StrengthBarVazio to Bar
					local topBar =  bar.TopBar
					-- resize bar based on newValue
				end)
			end
		end
	end
end
1 Like

Hello, thanks for help

I’m having some issues in the script,
Script:

Tables
image

Output:

i put some prints in script for test , and the script is just print the Defense, Strenght, Defense

i put some prints in script for test , and the script is just print the Defense, Strength, Defense

This is because line 70 prints from the stats table in the script and line 72 is printing stats from the StatsLabelFolder.

The error in the output comes from the stat Defense not existing in the players data folder. Can I see how you’re getting allStats?

1 Like

Helloo, i already solve the problem

local function update(statsPlayer, newValue, topBar, valueLabel)
	newValue = statsPlayer.Value/maxStats
	
	topBar:TweenSize(UDim2.new(newValue, 0, 0, 18), Enum.EasingDirection.Out, Enum.EasingStyle.Quint, 0, true)
	valueLabel.Text = tostring(statsPlayer.Value)
end

for _, stat in pairs(statsTable) do
	for _, statText in pairs(StatsLabelFolder)  do
		if statText:IsA("TextLabel") then
			if statText.Name == stat then
				
				newValue = allStats[stat].Value/maxStats
				
				local valueLabel = statText.ValueStats
				local bar = statText.Bar 
				local topBar =  bar.TopBar
				
				--Label settings
				valueLabel.Text = tostring(allStats[stat].Value)
				--TopBar settings
				topBar:TweenSize(UDim2.new(newValue, 0, 0, 18), Enum.EasingDirection.Out, Enum.EasingStyle.Quint, 0, true)
				topBar.BorderSizePixel = 0

				
				allStats[stat].Changed:Connect(function()
					update(allStats[stat], allStats[stat].Value/maxStats, topBar, valueLabel)
				end)
			end
		end
	end
end

this code works

thanks for help, i’ll put your comment as solution(sorry, i can’t put, because some devs will need the correct solution)

THANKSSSSSSS <3