Error help. How to fix

  1. What do you want to achieve? Keep it simple and clear!

so its not really me that needs help but since i have done some major things for my friends game i am asking for help about a error.

  1. What is the issue? Include screenshots / videos if possible!

so the error is

attempt to perform arithmetic (add) on nil and number

The code is

local Buttons = game.Workspace.Buttons

for i,button in pairs(Buttons:GetChildren()) do
	local ClickDetector = button.ClickDetector
	ClickDetector.MouseClick:Connect(function(player)
		button.BrickColor = BrickColor.new("Bright red")
		wait(1)
		button.BrickColor = BrickColor.new("Lime green")
		local Character = player.Character
		local number = string.sub(button.Name, 2)
		print(number)

                --This line below is the error on
		local DestinationName = "Stage"..tostring((tonumber(number)+1)).."TP"


		print(DestinationName)
		local Destination = game.Workspace.Teleports:FindFirstChild(DestinationName)
		print(Destination)
		if Destination then
			Character.HumanoidRootPart.CFrame = Destination.CFrame
		end
	end)
end

We are only wondering on how to fix this issue

What’s the value of the variable number and what did it print?
tonumber returns nil if the string argument supplied isn’t numerical.

(Also you don’t need tonumber or tostring for concatenation with numbers as strings and numbers are automatically converted to strings or numbers when doing most anything with each other)

1 Like

It prints the Button1 but without the b prints utton1

Try replacing

string.sub(button.Name, 2)

with

string.match(button.Name, "%d+")

The lower line will find the first sequence of numbers in the name.

Will try to do that but do i need “%d+” Didnt understand that basicly

"%d+" is a string pattern. It doesn’t tell string.match to look for the pattern itself in the string. Instead, it tells it to look for numbers in the string.
Edit: % tells that it’s a special pattern. There are different character classes. d spesifies that the character class which’s occurrences should be found is digits (numbers). + tells that it should return the whole sequence of characters belonging to the given character class if there are multiple of them without any other characters between them.

Thank you it works now thanks man