So I’m making a teleport system, but if you touch it it will teleport to the checkpoint. This is similar to the killblock system, if the killblock makes the character die, but my teleport system makes the player teleport to the checkpoint.
As in the picture above, I have created the script, and I am sure it is successful and runs smoothly, the output and analysis script does not show any warnings. However, when you touch the teleport part it doesn’t work.
local Checkpoints = workspace:WaitForChild("Checkpoints")
before teleport function, then on line 20 instead of workspace[nextCheckpointName] you should have Checkpoints:FindFirstChild(nextCheckpointName).
You could also just remove checkpointNames table and replace it with numberOfCheckpoints where you would store how many checkpoints you have then instead of searching for its name you can just convert nextCheckpointIndex to string with tostring().
Another thing i have noticed is that you are pointing to teleporter folder and checking if its been touched, which wont work. Instead you have to loop through the folder and connect a touched event to all of its children like this:
for i,v in teleporter:GetChildren() do
if not v:IsA("BasePart") then continue end
v.Touched:Connect(function()
--your code
end)
end
Your checkpoints are not located in the workspace instead they are located inside a folder named Checkpoints that is inside workspace, meaning you have to search that folder for the checkpoints and not the workspace (to get to the folder you can just change workspace[nextCheckpointName] with workspace.Checkpoints[nextCheckpointName], but to make sure it never errors its best to first point to that folder outside of the function, use WaitForChild to wait for it to load before pointing to it, and to use FindFirstChild when searching for a checkpoint).
You also don’t need to make a table with all the checkpoint names if their names are just numbers in order, just make a variable that holds the amount of checkpoints you have so you can check if current checkpoint is higher than amount of checkpoints you have, and you can just convert nextCheckpointIndex to a string with tostring() when you use it to search for a checkpoint inside of Checkpoints folder.
Another problem is that you are using .Touched on a folder, which doesn’t work, as you can only use .Touched on a Part. What you need to do instead is to loop through every child of the teleporter folder and connect a .Touched to all of them (as they are the parts that you want to teleport the player once they are touched).
You have to:
local checkpointNames --remove this table as you dont need it
local lastCheckpoint = 8 --add a variable which holds the last checkpoint number
local Checkpoints = workspace:WaitForChild("Checkpoints") --add a variable which points
--to the checkpoints folder
--in the function
--in the if statement replace #checkpointNames with lastCheckpoint
nextCheckpointIndex <= lastCheckpoint
local nextCheckpointName --remove this variable as you dont need it
--replace workspace[nextCheckpointName] with Checkpoints:FindFirstChild(tostring(nextCheckpointIndex))
local nextCheckpoint = Checkpoints:FindFirstChild(tostring(nextCheckpointIndex))
--instead of teleporter.Touched function
--loop through the teleporter folder instead
for i,v in teleporter:GetChildren() do --loop through all the children of the folder
if not v:IsA("BasePart") then continue end --if child is not a part skip to next child
--this is basically same as your current code just replace teleporter with v
v.Touched:Connect(function()
--your code
end)
end
Dude, I’ve tried your method but it doesn’t work. And now I make it again:
-- Define the Teleporter folder
local teleporterFolder = workspace:FindFirstChild("teleporter")
if not teleporterFolder then
teleporterFolder = Instance.new("Folder")
teleporterFolder.Name = "teleporter"
teleporterFolder.Parent = workspace
end
-- Create a function to teleport players to all checkpoints
local function teleportToAllCheckpoints(player)
local checkpointsFolder = workspace:FindFirstChild("Checkpoints")
if checkpointsFolder then
for i = 0, 7 do -- Assuming have 8 checkpoints named 0 through 7
local checkpoint = checkpointsFolder:FindFirstChild(tostring(i))
if checkpoint then
local character = player.Character
if character then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
humanoidRootPart.CFrame = checkpoint.CFrame
wait(1) -- Optional: Add a delay between teleportations
end
end
else
print("Checkpoint " .. i .. " not found!")
end
end
else
print("Checkpoints folder not found!")
end
end
-- Example: Teleport a player to all checkpoints
local player = game.Players.LocalPlayer -- Replace with the player you want to teleport
teleportToAllCheckpoints(player)
I’m not sure if anyone has mentioned this, but you should switch line 22 and 23, you should increase the number before teleporting the player to the next checkpoint.
That should work, however you are calling the function right as the server starts, meaning checkpoints might not be loaded, so its best to wait a bit before calling the function to make sure all checkpoints are loaded. You should move checkpointsFolder outside of teleportToAllCheckpoints function as you need to define it only once, also you should use WaitForChild instead of FindFirstChild on it.
It appears you are doing this in a LocalScript which should work (if the script is on the client), but its best to teleport the player from the server. All you would have to do is move the code into a Script (which would be located in ServerScriptService) and define player differently as you can’t use game.Players.LocalPlayer on the server. You could get the player by them stepping on a teleport part as you had it before, looping through game.Players table, etc…