Making a tool that teleports you to designated parts

Hello everybody. I’m trying to make a tool that teleports you to the start of the floor when clicked, and it cycles through all of the floors with one floor for each click. It detects the floor by a shared variable, but the code doesn’t do anything. Here are the two scripts in the tool

Regular script:

local tool = script.Parent
local mouse = game:GetService("UserInputService")
shared.FloorTPnum = 1

tool.Activated:Connect(function()
	shared.FloorTPnum += 1
end)

repeat
	repeat
	task.wait()
until shared.FloorTPnum == 11
shared.FloorTPnum = 1

until shared.FloorTPnum == 12

LocalScript:

local tool = script.Parent
local plr = game.Players.LocalPlayer
local ggg = plr:WaitForChild("PlayerGui").thing
local mouse = game:GetService("UserInputService")

tool.Equipped:Connect(function()
	ggg.Enabled = true
end)

tool.Activated:Connect(function()
	local LT = plr.Character.HumanoidRootPart
	if shared.FloorTPnum == 1 then
		LT.CFrame = game.Workspace.F1.CFrame
	end
end)

tool.Unequipped:Connect(function()
	ggg.Enabled = false
end)

I only have one teleport part at the moment (its name is F1 like in the script), but if I can get this working I will add more. How can I make the tool work as intended?

2 Likes

I decided to do some heavy rewriting of your script.

First, I started by making a Folder in game.Workspace and renamed it to “Floors”. I will put all floors inside of this folder. Make sure to name your floors how I named them right here.

Now, let’s start with the script. In our tool that is going to be used for teleporting the player, I have put a local script with the following code inside:

local tool = script.Parent
local plr = game.Players.LocalPlayer
local LT = plr.Character.HumanoidRootPart
local ggg = plr:WaitForChild("PlayerGui").thing
local debounce = false
local tpToFloor = 0
local maxFloor = 0

-- Checks how many floors are there
local floorsFolder = game.Workspace:WaitForChild("Floors")
for _, v in pairs(floorsFolder:GetDescendants()) do
	if v:IsA("Part") then -- if your floor isn't a part, change it to whatever it is e.g. UnionOperation
		maxFloor += 1
	end
end

tool.Equipped:Connect(function() -- Gui gets enabled after equip
	ggg.Enabled = true
end)

tool.Activated:Connect(function() -- When tool gets activated, fire the event for TP
	if not debounce then
		debounce = true
		task.delay(1, function() -- Cooldown for the tool, first arg is the time in seconds
			debounce = false
		end)

		if tpToFloor < maxFloor then
			tpToFloor += 1
			LT.CFrame = game.Workspace.Floors:FindFirstChild(tpToFloor).CFrame
		elseif tpToFloor == maxFloor then
			tpToFloor = 1
			LT.CFrame = game.Workspace.Floors:FindFirstChild(tpToFloor).CFrame
		end
	end
end)

tool.Unequipped:Connect(function() -- Gui gets disabled after unequip
	ggg.Enabled = false
end)

Edit: I added a cooldown function to your tool so it can’t be spammed.

Everything is explained inside of the code. The script is compatible with adding new floors without the need of editing the script, so this will be very helpful when you want to add new floors in the future.

Your tool is now ready for use! Sorry that you had to wait 2 days, but I hope I could help you!

Here is a video that demonstrates what the script does:

1 Like

actually, the character is owned by the client and does not necessarily need to be teleported by a server-sided script. and i’m not really sure why you need to use string values when you are seemingly only changing/reading their values within one script and could just use local variables instead. but if the script works, i guess you could just do whatever you want, it doesn’t really matter.

1 Like

Thanks for the feedback, I updated my post.

1 Like

Thank you for helping me! The only thing I’ll say is that your script didn’t work all the time so I rewrote it and changed a small amount of things.

local tool = script.Parent
local plr = game.Players.LocalPlayer
local ggg = plr:WaitForChild("PlayerGui").thing
local LT = plr.Character.HumanoidRootPart
local flort = game.Workspace:WaitForChild("Floors")
local flower = 1
local minflor = 1
local maxflor = 10

tool.Equipped:Connect(function()
	ggg.Enabled = true
end)

for _, i in pairs(flort:GetDescendants()) do
	if i:IsA("Part") then
		maxflor = 10
	end
end

tool.Activated:Connect(function()
	if flower < maxflor then
		flower += 1
		print(flower)
		LT.CFrame = flort:FindFirstChild(flower).CFrame
	elseif flower >= maxflor then
		flower = 1
		print(flower)
		LT.CFrame = flort:FindFirstChild(flower).CFrame
	end
end)

tool.Unequipped:Connect(function()
	ggg.Enabled = false
end)

(I name variables weird sorry)
The only thing I changed really is the v in pairs to i in pairs.
By the way would you like me to credit you? I will if you want

Weird that it didn’t work. Maybe you had some stuff named differently, that’s why it didn’t work. Glad that it works now though. There is no need to credit me btw :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.