Image button doesn't teleport player when clicked on

Hey i was just wondering if you know why this script doesn’t work
Its supposed to teleport the player to different obbies when clicking any image buttons inside of the table but only the buttontable[1] works at teleporting the player.:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local textButton = script.Parent
local screengui = script.Parent.Parent.Parent
local ImageLabel = script.Parent.Parent
local obby1 = game.Workspace.obby1
local obby2 = game.Workspace.obby2
local obby3 = game.Workspace.obby3
local obby4 = game.Workspace.obby4
local obby5 = game.Workspace.obby5
local obby6 = game.Workspace.obby6
local obby7 = game.Workspace.obby7
local obby8 = game.Workspace.obby8
local obby9 = game.Workspace.obby9
local obby10 = game.Workspace.obby10
local obby11 = game.Workspace.obby11
local obby12 = game.Workspace.obby12
local obby13 = game.Workspace.obby13
local obby14 = game.Workspace.obby14
local obby15 = game.Workspace.obby15
local obby16 = game.Workspace.obby16
local obby17 = game.Workspace.obby17
local obby18 = game.Workspace.obby18
local obby19 = game.Workspace.obby19
local obby20 = game.Workspace.obby20
local obby21 = game.Workspace.obby21
local background = script.Parent.Parent.Parent.Background
local CamPart = game.Workspace.CamPart -- The part that camera changes its view to
local SettingsButton = screengui.SettingsButton
local SettingsBackground = screengui.Settingsbackground
local ContinueButton = screengui.Settingsbackground.Continue1.Continue
local SettingsButton1 = screengui.Settingsbackground.Settings1.Settings
local LobbyButton = screengui.Settingsbackground.Lobby1.Lobby
local spawnlocation = game.Workspace.SpawnLocation
local currentcamera = game.Workspace.CurrentCamera
local folder = screengui.Imagebuttons
--table with all of the imagebuttons
local buttontable = {folder.ImageButton, folder.ImageButton1, folder.ImageButton2, folder.ImageButton3, folder.ImageButton4, folder.ImageButton5, folder.ImageButton6, folder.ImageButton7, folder.ImageButton8, folder.ImageButton9, folder.ImageButton10, folder.ImageButton11, folder.ImageButton12, folder.ImageButton13, folder.ImageButton14, folder.ImageButton15, folder.ImageButton16, folder.ImageButton17, folder.ImageButton18, folder.ImageButton19, folder.ImageButton20}


buttontable[2].MouseButton1Click:Connect(function()
	for _, button in ipairs(buttontable) do
		button.Visible = false
	end
	background.Visible = false
	wait(0.5)
	humanoidRootPart.CFrame = obby2.CFrame + Vector3.new(0, 5, 0)
	currentcamera.CFrame = character.Head.CFrame
	currentcamera.FieldOfView = 70
	currentcamera.CameraType = Enum.CameraType.Custom
	SettingsButton.Visible = true
end)

buttontable[3].MouseButton1Click:Connect(function()
	for _, button in ipairs(buttontable) do
		button.Visible = false
	end
	background.Visible = false
	wait(0.5)
	humanoidRootPart.CFrame = obby3.CFrame + Vector3.new(0, 5, 0)
	currentcamera.CFrame = character.Head.CFrame
	currentcamera.FieldOfView = 70
	currentcamera.CameraType = Enum.CameraType.Custom
	SettingsButton.Visible = true
end) 

and so on until buttontable[21]

16 Likes

Hi! This is a little bit unrelated to your problem, but this is actually extremely important.

I have seen this SO many times and you are not alone.

Please take this entire post as constructive criticism, and I am NOT trying to be rude.

Your code is very inefficient and hard to read for me to understand the problem.

Currently, your code is in a single script (as I can see it) which defines 21 variables, and has 21 different functions, one for each.

However, this approach can cause clutter, unnecessary confusion, and in rare cases, even game lag. So, if we want a solution that is more simple to read and avoids these problems, we should improvise. Once we improvise, finding the problem will be a lot easier.

Let’s begin with your variables starting from

local obby1 = game.Workspace.obby1

That is 21 lines, and can be made a LOT simpler.

Firstly, we will simplify the obbies.

Move your obbies into a workspace folder called “Obbies”.

Now, rename your obbies to their name. Instead of obby1, replace that with 1, 2, 3, etc.

We can now delete those 21 lines as they will be useless in the code.

In fact, most of this script can be moved into several smaller scripts.

In each ImageButton, create one script.

Let’s start off with the variables.

local player = game.Players.LocalPlayer
local character = player.Character
local obbiesFolder = game.Workspace.Obbies
local imageButton = script.Parent

As you can see, we have already eliminated most of the variables as we no longer need them.

And yes, there is no need for an imageButton table this script is in every image button.

Now, we can add one single occurrence of the teleportation. (I simplified the code there a little bit for readability)

Also, in the code below, you will have to define background as I do not know where your background is stored. If there are any other variables missing, you can define it as an additional variable above. This includes current camera.

imageButton.MouseButton1Click:Connect(function()
	for _, button in pairs(imageButton.Parent) do
		button.Visible = false
	end
	background.Visible = false 
	wait(0.5)
	character.HumanoidRootPart.CFrame = obby2.CFrame + Vector3.new(0, 5, 0)
	currentcamera.CFrame = character.Head.CFrame
	currentcamera.FieldOfView = 70
	currentcamera.CameraType = Enum.CameraType.Custom
	SettingsButton.Visible = true
end)

I hope this helps you, and I wish you well on your programming journey.

If there is anything additional for me to explain, please let me know.

If there are any variables which are not working or defined, see above, or add them.

7 Likes

It’s probably better to use a loop instead of do it for every single button, or at least create some kind of function. Also, since this is a local script, it might not work correctly. Can you show us the script for buttontable[1]?
Are there any errors?
Also, one problem might be

humanoidRootPart.CFrame = obby2.CFrame + Vector3.new(0, 5, 0)

Since you are trying to add a Vector3 to a CFrame. To solve this, you can try to do:

humanoidRootPart.CFrame = CFrame.new(obby2.CFrame.Position + Vector3.new(0, 5, 0))

or any other method.

7 Likes

So i should put a script in each imagebutton instead of a table?
If so would this script work:

local player = game.Players.LocalPlayer
local character = player.Character
local obbiesFolder = game.Workspace.Obbies
local imageButton = script.Parent
local background = game.StarterGui.ScreenGui.Background
local currentcamera = game.Workspace.CurrentCamera
local SettingsButton = game.StarterGui.ScreenGui.SettingsButton

imageButton.MouseButton1Click:Connect(function()
	for _, button in pairs(imageButton.Parent) do
		button.Visible = false
	end
	background.Visible = false 
	wait(0.5)
	character.HumanoidRootPart.CFrame = obbiesFolder.obby2.CFrame + Vector3.new(0, 5, 0)
	currentcamera.CFrame = character.Head.CFrame
	currentcamera.FieldOfView = 70
	currentcamera.CameraType = Enum.CameraType.Custom
	SettingsButton.Visible = true
end)
4 Likes

Yes, and with that you can delete the old script. If this solution does not work, please let me know with more information so I can better assist you.

5 Likes

Okay so i put this script:

local player = game.Players.LocalPlayer
local character = player.Character
local obbiesFolder = game.Workspace.Obbies
local imageButton = script.Parent
local background = game.StarterGui.ScreenGui.Background
local currentcamera = game.Workspace.CurrentCamera
local SettingsButton = game.StarterGui.ScreenGui.SettingsButton

imageButton.MouseButton1Click:Connect(function()
	for _, button in pairs(imageButton.Parent) do
		button.Visible = false
	end
	background.Visible = false 
	wait(0.5)
	character.HumanoidRootPart.CFrame = obbiesFolder.obby2.CFrame + Vector3.new(0, 5, 0)
	currentcamera.CFrame = character.Head.CFrame
	currentcamera.FieldOfView = 70
	currentcamera.CameraType = Enum.CameraType.Custom
	SettingsButton.Visible = true
end)

in every imagebutton

I still needed the old script so it shows the text button (which is the “play” button that appears at the start, it makes the background and the image buttons appear). Here’s the old script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local textButton = script.Parent
local ImageLabel = script.Parent.Parent
local background = script.Parent.Parent.Parent.Background
local CamPart = game.Workspace.CamPart -- The part that camera changes its view to
local spawnlocation = game.Workspace.SpawnLocation
local currentcamera = game.Workspace.CurrentCamera
local folder = game.StarterGui.ScreenGui.Imagebuttons
--table with all of the imagebuttons
local buttontable = {folder.ImageButton, folder.ImageButton1, folder.ImageButton2, folder.ImageButton3, folder.ImageButton4, folder.ImageButton5, folder.ImageButton6, folder.ImageButton7, folder.ImageButton8, folder.ImageButton9, folder.ImageButton10, folder.ImageButton11, folder.ImageButton12, folder.ImageButton13, folder.ImageButton14, folder.ImageButton15, folder.ImageButton16, folder.ImageButton17, folder.ImageButton18, folder.ImageButton19, folder.ImageButton20}

wait(0.1)
ImageLabel.Visible = true
currentcamera.CameraType = Enum.CameraType.Scriptable
currentcamera.FieldOfView = 55
currentcamera.CFrame = CamPart.CFrame -- changes the cameras view to the view of the part

textButton.MouseButton1Click:Connect(function()
	textButton.Visible = false
	ImageLabel.Visible = false
	background.Visible = true
	for _, button in ipairs(buttontable) do
		button.Visible = true
	end
end)

The problem is now that when i click the play button(textbutton) the background appears but the imagebuttons do not.
Here’s a video so its easier to understand:

4 Likes

In your second script, you are missing a for loop which loops through the text buttons and makes them visible. You can do a for loop through the table in the second script to make them visible.

1 Like

Theres only one text button and it’s visible (the “Play” button at the start) I am trying to make image buttons visible. If i understood you correctly.

2 Likes

Sorry, I meant to say image buttons instead of text buttons. Make a for loop and make them visible, this should fix your problems.

1 Like

but isn’t this already making the imagebuttons visible?

1 Like

I am not able to see fully what the problem is, but there’s a few things. Try to increase the ZIndex of all the image buttons and make sure they are higher than the ZIndex of the background.

2 Likes

Hi! I didn’t not read most of the comments yet, but I understand the script. You are saying buttontable[21], aka ImageButton20, is not working despite all the others working? I do not recommend putting each script individually inside each individual button that’s just messy. What DevHapple mention, your script being messy, is not true, it is actually good, could be written more simpler, way simpler, but it works and doesn’t loop, hence not making your game lag. That aside I suggest you try seeing if the Text name is correct for ImageButton20, sometimes people mistype the name wrong making it unable to work. If that also doesn’t work, I would also suggest check if a variable does exist, if it doesn’t it can also cause the script to not work. I would also like the output screenshot of this original script not working.

1 Like

That is not what I am trying to achieve, all of the buttons except buttontable[1] didn’t work but we are far past that point. And the script was very messy

1 Like

I have to disagree with this comment.

Listing out 21 different variables and 21 different functions is very messy and inefficient. I believe it is better to have smaller scripts, which is still not the best, but best for the intended functionality.

3 Likes

Already did it, zindex on the image buttons is 2 and the layout order is 2, while on the background zindex is 1 and layout order is 1

1 Like

Interesting. Can you check while running the game to see if the visible setting is checked on? While you do this, I will continue reading through your code to look for any possible triggers.

1 Like

It says visible when playing:

If this is the case, the script is working as intended and there may be a different problem. Having the buttons in folders should not make a difference. The code is working as intended from what I can see.

1 Like

This was assuming the creator, most of which are newbie creators on here, are making their games, it’s a good start, it’s just the same script repeated over and over again for the next 100 or so lines. That aside I’m curious as to why not try using

for i, v in ipairs(folder:GetChilren()) do
if v:IsA("ImageButton") then
v.Visible == true
end
end
2 Likes

Shouldn’t it be only one “=” sign at

I tried it and this error came out: GetChilren is not a valid member of Folder “StarterGui.ScreenGui.Imagebuttons”

1 Like