Why does it give me an error when I try to change the text?

I’m trying to make a script that has a text button have the updated values that are in the table. When I run it it gives me the error, “attempt to concatenate string with nil”. How would I fix this error?

local values= {
	
{0},
{25},
{50},
{100},
{150},
{200},
{300},
}
local player = game.Players.LocalPlayer
	local points= player.leaderstats.Points
	local text = script.Parent.Text
	for i, v in pairs(values) do
	points.Changed:Connect(function()
		if points.Value >= v[1] then
			text = "Next evolution:"..v[2]
		end
	end)
	end

This is because v[2] simply doesn’t exist. In the values table, there are tables and each of those sub-table only have one number. You are calling for another number which doesn’t exist since there’s only one.

Also another issue I’m seeing is that you are actually assigning the variable text with a string, not the .Text property of the TextButton itself.

I’m gonna assume that this is what you wanted to do:

local values= {
  0,
  25,
  50,
  100,
  150,
  200,
  300,
}
local player = game.Players.LocalPlayer
local points= player.leaderstats.Points
local text = script.Parent
points.Changed:Connect(function()
  for k, v in ipairs(values) do --using ipairs so it iterates in order
    if points.Value < v then
      text.Text = "Next evolution: "..v
    end
  end
end)

When I run that it gives me the error, “attempt to compare number < to table”.

Did you also change the values table to what I use?

Oh, no I wasn’t. Using them seems to work great.

I just noticed that it shows the last value in the table instead of the next value on the text button. So instead of it going to 25, it goes to 300. How would I fix this?

I think the script above may have an error(showing the current evolution) so I decided to make a few upgrades:

local Players = game:GetService("Players")

local Player = Players.LocalPlayer

--when storing single values in an array you can just seperate them with a comma
local values = {0, 25, 50, 100, 150, 200, 300}

--gives 5 seconds to leaderstats and points to load, if they fail the script wont run and an error will be shown.
local leaderstats = Player:WaitForChild("leaderstats", 5) 
if not leaderstats then warn("leaderstats didn't load") return end 
local Points = leaderstats:WaitForChild("Points", 5) 
if not Points then warn("Points didn't load") return end 

--you cannot change text directly you have to store the reference to the object
local TextLabel = script.Parent

--loops through table and gets current evolution index
function GetEvolution(value)
	local evo = 1 
	for i, v in pairs(values) do 
		if value <= v then 
			evo = i 
		end
	end
	return evo  
end

--.Changed returns the updated value
Points.Changed:Connect(function(value)
	local evo = GetEvolution(value) 
	local nextEvo = values[evo+1]
	if not nextEvo then 
		nextEvo = "max"
	end
	TextLabel.Text = "Next evolution: "..nextEvo
end)

I believe using ipairs like I did will run the loop in order so it should iterate from 0 to 300. Can you try printing out the value and show me the results?

Put this under the for loop:

print(v)

It printed out all of the values instantly in the output.

Screenshot the output and post it here

image

Then I’m not really sure what the problem is. It compares the point with each of those values, from top to bottom. If the value in the table is higher than the points, then that value is the next evolution requirement. Can you also try printing out points.Value?

It’s rapidly printing the values of the points.
image

Ah I see the problem. Can’t believe I missed it. You also need to use the break keyword to stop the loop once it finds the requirement.

local player = game.Players.LocalPlayer
local points= player.leaderstats.Points
local text = script.Parent
points.Changed:Connect(function()
  for k, v in ipairs(values) do --using ipairs so it iterates in order
    if points.Value < v then
      text.Text = "Next evolution: "..v
      break --added this
    end
  end
end)

Ohh, that makes sense, thanks for the help it seems to work great now.