Tonumber() isn't working

So I am making a slideshow operator in roblox where you can choose a slideshow you want to display on a television and control it by clicking GUI buttons for next slide, previous slide, etc. For the control panel to change the slide, I am using this script I made:

script.Parent.MouseButton1Click:Connect(function()
	local slide = game.Workspace.Television.Slideshow:FindFirstChildOfClass("Decal")
	local currentslide = slide.Name
	tonumber(currentslide)
	local newslide = currentslide + 1
	local slideshowvalue = game.ReplicatedStorage.Slideshows.CurrentSlideshow.Value
	local currentslideshow = game.ReplicatedStorage.Slideshows:FindFirstChild(slideshowvalue)
	print(newslide)
	local maxslides = currentslideshow.MaxSlides.Value
	if currentslide < maxslides then
		tostring(newslide)
		slide:Destroy()
		local duplicate = currentslide:FindFirstChild(newslide)
		duplicate:Clone()
		duplicate.Parent = game.Workspace.Television.Slideshow
	end
end)

The problem is with this line right here:

if currentslide < maxslides then

It says that I am trying to compare a string with an int value when I am converting currentslide to an int value with this line of code right here:

tonumber(currentslide)

Also, before you ask if maxslides is a string value, it is an IntValue located in replicatedstorage so that is not the issue.
I know there is most likely an easy fix that hasn’t caught my eye, so if you could help me, it would me much appreciated. Thanks!

change that to

currentslide  = tostring(newslide)

I did a form of what you said and came with this new script

script.Parent.MouseButton1Click:Connect(function()
	local slide = game.Workspace.Television.Slideshow:FindFirstChildOfClass("Decal")
	local currentslide = slide.Name
	currentslide = tonumber(currentslide)
	local newslide = currentslide + 1
	local slideshowvalue = game.ReplicatedStorage.Slideshows.CurrentSlideshow.Value
	local currentslideshow = game.ReplicatedStorage.Slideshows:FindFirstChild(slideshowvalue)
	print(newslide)
	local maxslides = currentslideshow.MaxSlides.Value
	if currentslide < maxslides then
		newslide = tostring(newslide)
		slide:Destroy()
		local duplicate = currentslide:FindFirstChild(newslide)
		duplicate:Clone()
		duplicate.Parent = game.Workspace.Television.Slideshow
	end
end)

Now, it is making it past the if statement like I want it to but the problem is in this line:

local duplicate = currentslide:FindFirstChild(newslide)

Now it says newslide is an int, not a string when I am changing it to a string here:

newslide = tostring(newslide)

What do I do?

Looks like currentslide is not an Instance, so you’d be unable to call FindFirstChild on that line you’re referring to unless you changed currentslide back to an Instance.

I would recommend just running local duplicate = slide:FindFirstChild(tostring(newslide)) and that should make it work.

Now the issue is that it is trying to find nil.
This is the srcript I am using now:

script.Parent.MouseButton1Click:Connect(function()
	local slide = game.Workspace.Television.Slideshow:FindFirstChildOfClass("Decal")
	local currentslide = slide.Name
	currentslide = tonumber(currentslide)
	local newslide = currentslide + 1
	local slideshowvalue = game.ReplicatedStorage.Slideshows.CurrentSlideshow.Value
	local currentslideshow = game.ReplicatedStorage.Slideshows:FindFirstChild(slideshowvalue)
	local maxslides = currentslideshow.MaxSlides.Value
	if currentslide < maxslides then
		slide:Destroy()
		local duplicate = slide:FindFirstChild(tostring(newslide))
		print(newslide)
		duplicate:Clone()
		duplicate.Parent = game.Workspace.Television.Slideshow
	end
end)

I know I said this before, but it is trying to clone a nil value. I added this print statement to try and debug it but it is returning “2” not nil.
Heres the print statement I used:

print(tostring(newslide))

And this is also to show that there is something called 2 in the model:
image
I have no idea why it isn’t working

That makes sense cause you destroy the slide and then expect it to have a child, which is impossible.

I recommend to rewrite the script with functions. The reason why you are getting these cryptic errors is because your code is not very readable.

local currentSlide = THEFIRSTTELEVISIONSLIDE
local slideShow = THESLIDESHOWYOUNEED

script.Parent.MouseButton1Click:Connect(function()
	local nextSlide = getNextSlide(slideShow, currentSlide)
	displaySlide(nextSlide)
end)

Ah, well if the name of the child you are trying to find is just “2”, you don’t even need tostring() in the first place.

Also, you’re Destroying slide before trying to find the next child of it in your script now. Not sure if I’ve messed it up for your means, but you won’t be able to find anything in slide now that you’ve destroyed it.

I am making a slideshow operator that can go to the next slide, previous slide, etc. Its not always going to be “2”, it could be 4, 3, etc. That’s why I am making it this way.
I changed the script to this so the current slide is destroyed AFTER the new slide is cloned but it is still saying it is attempting to index nil with clone. Heres the script:

script.Parent.MouseButton1Click:Connect(function()
	local slide = game.Workspace.Television.Slideshow:FindFirstChildOfClass("Decal")
	local currentslide = slide.Name
	currentslide = tonumber(currentslide)
	local newslide = currentslide + 1
	local slideshowvalue = game.ReplicatedStorage.Slideshows.CurrentSlideshow.Value
	local currentslideshow = game.ReplicatedStorage.Slideshows:FindFirstChild(slideshowvalue)
	local maxslides = currentslideshow.MaxSlides.Value
	if currentslide < maxslides then
		
		local duplicate = slide:FindFirstChild(tostring(newslide))
		print(tostring(newslide))
		duplicate:Clone()
		duplicate.Parent = game.Workspace.Television.Slideshow
		slide:Destroy()
	end
end)

Yes, and as long as it’s just the number you’re looking for, you won’t need tostring(), but that’s not your problem here it seems. I think you’re trying to duplicate the slide from currentslideshow, so run the following and see if it works. If it doesn’t, let me know what outputs and we can go from there.

Edit: Accidentally left the second duplicate:Clone() in there. Make sure you take that out.

script.Parent.MouseButton1Click:Connect(function()
	local slide = game.Workspace.Television.Slideshow:FindFirstChildOfClass("Decal")
	local currentslide = tonumber(slide.Name)
	print("Current Slide:",currentslide)
	local slideshowvalue = game.ReplicatedStorage.Slideshows.CurrentSlideshow.Value
	local currentslideshow = game.ReplicatedStorage.Slideshows:FindFirstChild(slideshowvalue)
	if currentslide < currentslideshow.MaxSlides.Value then
		local duplicate = currentslideshow[currentslide+1]:Clone()
		duplicate.Parent = game.Workspace.Television.Slideshow
		slide:Destroy()
	end
end)

It works! Thank you so much for helping!

No problem! Let me know if you need anything else