Help with my teleport script

Alright, so pretty much I want to make a thing at spawn where when you press it a GUI pops up and you can choose a location to teleport to. Depending on the button you press, it will show you what that area looks like so you can check if there are any enemies there before you confirm. It works just fine, until the character dies and resets.

Here’s the script (and yes it is a local script):

local RPS = game:GetService("ReplicatedStorage")
local tpEvent = RPS:WaitForChild("tpToPartEvent") -- remote event
local camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local boxes = script.Parent.Parent.PlayerGui:WaitForChild("tpPrompt").Frame:GetChildren()
local Buttons = {}

for _, v in pairs(boxes) do
	if v:IsA("TextButton") then
		table.insert(Buttons, v)
	end
end

repeat wait() until Player.Character --waits for character

for _, v in pairs(Buttons) do
	
	local number = v:GetAttribute("number") --each button has an attribute with its number
	
	v.MouseButton1Click:Connect(function()
		camera.CameraSubject = game.Workspace.tpParts:FindFirstChild("tpPart" .. number) --the corresponding part with the same number as the button
		camera.CameraType = Enum.CameraType.Orbital
		
		v.Parent.Visible = false
		v.Parent.Parent.confCanc.Visible = true --gets rid of the butttons and prompts a "confirm/cancel" thingy to confirm they want to teleport there
		
		v.Parent.Parent.confCanc.conf.MouseButton1Click:Connect(function()
			-- reset the whole GUI back to how it was before and tells the server to teleport the player to the part
			v.Parent.Visible = true
			v.Parent.Parent.confCanc.Visible = false
			v.Parent.Parent.Enabled = false
			camera.CameraSubject = Player.Character
			camera.CameraType = Enum.CameraType.Custom
			tpEvent:FireServer(number)
		end)
		
		v.Parent.Parent.confCanc.canc.MouseButton1Click:Connect(function()
			--reset the whole GUI back to how it was before
			v.Parent.Visible = true
			v.Parent.Parent.confCanc.Visible = false
			v.Parent.Parent.Enabled = false
			camera.CameraSubject = Player.Character
			camera.CameraType = Enum.CameraType.Custom
		end)
		
	end)
end


Whenever the character resets, none of the buttons do anything. The GUI still pops up but that’s probably just because a second script handles that. I assume the fact that it uses for loops instead of a separate script for each individual buttons might cause some issues, but I would like to keep the amount of scripts running to a minimum. Any suggestions?

1 Like

Have you tried setting the ResetOnSpawn property to true…?

Also, there’s a much more easier way of referencing the PlayerGui instead of doing this:

You could just do

local boxes = Player.PlayerGui:WaitForChild("tpPrompt").Frame:GetChildren()
1 Like

So I didn’t know about the ResetOnSpawn property was a thing, but it was already on. It turns out turning it off, however, fixes my issue. Thanks!