Getting value from for loop to change a value

Hey, right now i’m trying to get a specific value from a for loop, and check if names align from other textlabel names

		local n = math.random(1,3)
		for i,v in Mineables do
			table.insert(tabel,Mineables[i])
			
			if n == i then
				Mineables[i].Visible = true
				previousframe = Mineables[i]
			end
		end

ive tried using table.insert, to get the values in 1 table, which worked but i still had to try to change textlabel names
image

as example, if the dirt frame would be called(its every time i click, its 1/3 for one of the frames to get shown) i want to change the value in dirt to be +1.

i just cant quite get that last part to work, because i cant get the specific value of the one that got called.
in short, im trying to get a value from a for loop, bring it out of the for loop, get the specific value that matches to one of the textlabels, change the value in the text label to +1, im a bit tired right now so if u dont understand something i said, or whatever, just ask me lol, i have a strong feeling half of this doesnt make sence

2 Likes

You need to access the value inside the TextLabel’s child (labeled “Value” in your hierarchy) when a specific frame is selected.

-- Randomly pick a mineable frame
local n = math.random(1, #Mineables) -- Assuming Mineables is an array of frames

-- Loop through the Mineables frames
for i, frame in ipairs(Mineables) do
    if i == n then
        -- Make the selected frame visible
        frame.Visible = true
        
        -- Find the corresponding text label and increase its value
        local frameName = frame.Name -- Get the frame's name (e.g., "Dirt", "Grass", "Stone")
        
        -- Assuming you have corresponding TextLabels in your GUI that follow the same names as the frames
        local textLabel = game.Gui.GameGUI.Materials:FindFirstChild(frameName) -- Find the matching TextLabel
        
        if textLabel then
            local valueLabel = textLabel:FindFirstChild("Value") -- Find the Value object within the TextLabel
            
            if valueLabel and valueLabel:IsA("TextLabel") then
                -- Convert the text value to a number, add 1, and update the text
                local currentValue = tonumber(valueLabel.Text) or 0
                valueLabel.Text = tostring(currentValue + 1)
            end
        end
        
        -- Store the frame for later use
        previousFrame = frame
    end
end

Im not quite getting this, the biggest issue that if fixed, would fixed my issue, is that the for loop(when going through 3 instances) added 3 when i do +1, because it loops 3 times

-- Randomly pick a mineable frame
local n = math.random(1, #Mineables) -- Assuming Mineables is an array of frames

-- Loop through the Mineables frames
for i, frame in ipairs(Mineables) do
    if i == n then
        -- Make the selected frame visible
        frame.Visible = true
        
        -- Find the corresponding text label and increase its value
        local frameName = frame.Name -- Get the frame's name (e.g., "Dirt", "Grass", "Stone")
        
        -- Assuming you have corresponding TextLabels in your GUI that follow the same names as the frames
        local textLabel = game.Gui.GameGUI.Materials:FindFirstChild(frameName) -- Find the matching TextLabel
        
        if textLabel then
            local valueLabel = textLabel:FindFirstChild("Value") -- Find the Value object within the TextLabel
            
            if valueLabel and valueLabel:IsA("TextLabel") then
                -- Convert the text value to a number, add 1, and update the text
                local currentValue = tonumber(valueLabel.Text) or 0
                valueLabel.Text = tostring(currentValue + 1)
            end
        end
        
        -- Break out of the loop after finding the matching frame and updating the value
        break
    end
end

Not really understanding you but I added a break.

okay maybe what would help is giving u more information

local plr = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

local GameGUI = plr.PlayerGui:WaitForChild("GameGUI")
local PickaxeGUI = plr.PlayerGui.PickaxeGUI
local Pickaxe = PickaxeGUI.Pickaxe

local MP = 0 --MinePoints
local TMP = 0 --Total MinePoints
local tmpLabel = GameGUI.TotalMP.TotalMP
local mpLabel = GameGUI.MP.MP

mpLabel.Text = MP .. " MP"
tmpLabel.Text = TMP .. " Total MP"

local mineCDtime = 0.1
local mineCD = 0

local Materials = GameGUI.Materials:GetChildren()



local Mineables = GameGUI.Mineables:GetChildren()
print(Mineables)

GameGUI.Mineables.Grass.Visible = true

local previousframe
local tabel = {}
UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and mineCD <= tick() then
		mineCD = tick() + mineCDtime
		TMP = TMP+1
		MP = MP+1
		mpLabel.Text = MP .. " MP"
		tmpLabel.Text = TMP .. " Total MP"

		
		local tweenInfoGo = TweenInfo.new(
			0.03,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local tweenInfoBack = TweenInfo.new(
			0.06,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local tweenInfoSTARTPOS = TweenInfo.new(
			0.03,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local TweenGo = TweenService:Create(Pickaxe, tweenInfoGo, {Rotation = 5})
		TweenGo:Play()
		wait(0.03)
		local TweenBack = TweenService:Create(Pickaxe, tweenInfoBack, {Rotation = -5})
		TweenBack:Play()
		wait(0.06)
		local TweenSTART = TweenService:Create(Pickaxe, tweenInfoSTARTPOS, {Rotation = 0})
		TweenSTART:Play()
		
		print(tabel)
		
		print(previousframe)
		if previousframe then
			previousframe.Visible = false
			previousframe = nil
		end
		local n = math.random(1,3)
		for i,v in Mineables do
			
			
			if n == i then
				Mineables[i].Visible = true
				previousframe = Mineables[i]
			end
		end


	end
	
end)

this is the localscript thats in StarterPlayer
image
and this screenshot again

My issue here is that when i click, it shows me 1 of the 3 frames which are, dirt,grass, or stone, for example if it shows stone, and i click again, i want it to add 1 to my stones
image
but the problem im having is, i cant make it so that it will add +1 to the stone when i click on the stone frame that shows, just putting it in for i,v in Mineables do will add 3, because it loops through 3 different frames
i hope this helps :slight_smile:

Try this

local plr = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

local GameGUI = plr.PlayerGui:WaitForChild("GameGUI")
local PickaxeGUI = plr.PlayerGui.PickaxeGUI
local Pickaxe = PickaxeGUI.Pickaxe

local MP = 0 -- MinePoints
local TMP = 0 -- Total MinePoints
local tmpLabel = GameGUI.TotalMP.TotalMP
local mpLabel = GameGUI.MP.MP

mpLabel.Text = MP .. " MP"
tmpLabel.Text = TMP .. " Total MP"

local mineCDtime = 0.1
local mineCD = 0

local Materials = GameGUI.Materials:GetChildren()

local dirtValueLabel = GameGUI.Materials.Dirt.Value
local grassValueLabel = GameGUI.Materials.Grass.Value
local stoneValueLabel = GameGUI.Materials.Stone.Value

local dirtValue = tonumber(dirtValueLabel.Text)
local grassValue = tonumber(grassValueLabel.Text)
local stoneValue = tonumber(stoneValueLabel.Text)

local Mineables = GameGUI.Mineables:GetChildren()

local previousframe
local tabel = {}

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and mineCD <= tick() then
		mineCD = tick() + mineCDtime
		TMP = TMP + 1
		MP = MP + 1
		mpLabel.Text = MP .. " MP"
		tmpLabel.Text = TMP .. " Total MP"

		-- Tween for Pickaxe animation
		local tweenInfoGo = TweenInfo.new(0.03, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
		local tweenInfoBack = TweenInfo.new(0.06, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
		local tweenInfoSTARTPOS = TweenInfo.new(0.03, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
		
		local TweenGo = TweenService:Create(Pickaxe, tweenInfoGo, {Rotation = 5})
		TweenGo:Play()
		wait(0.03)
		local TweenBack = TweenService:Create(Pickaxe, tweenInfoBack, {Rotation = -5})
		TweenBack:Play()
		wait(0.06)
		local TweenSTART = TweenService:Create(Pickaxe, tweenInfoSTARTPOS, {Rotation = 0})
		TweenSTART:Play()

		if previousframe then
			previousframe.Visible = false
			previousframe = nil
		end
		
		local n = math.random(1, 3)
		for i, v in ipairs(Mineables) do
			if n == i then
				Mineables[i].Visible = true
				previousframe = Mineables[i]
			end
		end
		
		-- Check the currently visible frame and increment the correct material's value
		if previousframe then
			if previousframe.Name == "Dirt" then
				dirtValue = dirtValue + 1
				dirtValueLabel.Text = tostring(dirtValue)
			elseif previousframe.Name == "Grass" then
				grassValue = grassValue + 1
				grassValueLabel.Text = tostring(grassValue)
			elseif previousframe.Name == "Stone" then
				stoneValue = stoneValue + 1
				stoneValueLabel.Text = tostring(stoneValue)
			end
		end
	end
end)

sorry i forgot to add one detail, i wanna make it so that i can get new worlds, so i cant just have dirt, stone, and grass in every world, so i cant specifically call stone,grass, or dirt

  • Associate each frame (like Dirt, Grass, Stone) with its corresponding value dynamically. This can be done by getting the Value child of the material.
local plr = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

local GameGUI = plr.PlayerGui:WaitForChild("GameGUI")
local PickaxeGUI = plr.PlayerGui.PickaxeGUI
local Pickaxe = PickaxeGUI.Pickaxe

local MP = 0 -- MinePoints
local TMP = 0 -- Total MinePoints
local tmpLabel = GameGUI.TotalMP.TotalMP
local mpLabel = GameGUI.MP.MP

mpLabel.Text = MP .. " MP"
tmpLabel.Text = TMP .. " Total MP"

local mineCDtime = 0.1
local mineCD = 0

local Mineables = GameGUI.Mineables:GetChildren()
local previousframe
local tabel = {}

UIS.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 and mineCD <= tick() then
        mineCD = tick() + mineCDtime
        TMP = TMP + 1
        MP = MP + 1
        mpLabel.Text = MP .. " MP"
        tmpLabel.Text = TMP .. " Total MP"

        -- Tween for Pickaxe animation
        local tweenInfoGo = TweenInfo.new(0.03, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
        local tweenInfoBack = TweenInfo.new(0.06, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
        local tweenInfoSTARTPOS = TweenInfo.new(0.03, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
        
        local TweenGo = TweenService:Create(Pickaxe, tweenInfoGo, {Rotation = 5})
        TweenGo:Play()
        wait(0.03)
        local TweenBack = TweenService:Create(Pickaxe, tweenInfoBack, {Rotation = -5})
        TweenBack:Play()
        wait(0.06)
        local TweenSTART = TweenService:Create(Pickaxe, tweenInfoSTARTPOS, {Rotation = 0})
        TweenSTART:Play()

        -- Hide previous frame
        if previousframe then
            previousframe.Visible = false
            previousframe = nil
        end

        -- Pick a random frame to show
        local n = math.random(1, #Mineables)
        for i, v in ipairs(Mineables) do
            if n == i then
                Mineables[i].Visible = true
                previousframe = Mineables[i]
            end
        end

        -- Check the currently visible frame and increment the corresponding value dynamically
        if previousframe then
            -- Find the corresponding material label in Materials (by matching the Name of the frame)
            local materialLabel = GameGUI.Materials:FindFirstChild(previousframe.Name)
            
            if materialLabel then
                -- Find the Value child and increment it
                local valueLabel = materialLabel:FindFirstChild("Value")
                
                if valueLabel and valueLabel:IsA("TextLabel") then
                    local currentValue = tonumber(valueLabel.Text) or 0
                    valueLabel.Text = tostring(currentValue + 1)
                end
            end
        end
    end
end)

Changes:

  1. Now use previousframe.Name to find the corresponding material dynamically.
  2. Once the material is found, the script looks for the Value child inside that material and increments it by 1.

the problem is that you are trying to use a loop for Mineables, but instead of a loop you should use GetChildren() instead because then it will give you a list of the frames under mineables

local n = math.random(1, 3)
local mineablesTable = Mineables:GetChildren()

for i, frame in ipairs(mineablesTable) do
    table.insert(tabel, frame)
    
    if n == i then
        frame.Visible = true
        previousframe = frame

        local valueObject = frame:FindFirstChild("Value")
        if valueObject and valueObject:IsA("NumberValue") then
            valueObject.Value = valueObject.Value + 1
        end
    else
        frame.Visible = false
    end
end
1 Like

So at

                if valueLabel and valueLabel:IsA("TextLabel") then
                    local currentValue = tonumber(valueLabel.Text) or 0
                    valueLabel.Text = tostring(currentValue + 1)
                end

It doesnt work unless i make it if valueLabel and valueLabel:IsA("NumberValue") then and change valueLabel.Text to valueLabel.Value
This changes the value, but the 2 issues right now is that, when i click, and the frame turns into dirt, it adds +1 to dirt and not the previous one.
the other issue is that the label isnt changing, but thats an issue with the scripts in the labels themselfs