I created a system where when you click a button, it gets your checkpoint value and then gets each checkpoint before that and allows you to teleport to it. Basically, if you have reached checkpoint 5 and you click the teleport button, it will clone 5 buttons: checkpoint 1, checkpoint 2, checkpoint 3, checkpoint 4, and checkpoint 5, and when you click the buttons, it teleports you to that checkpoint. The only problem is that when the player is teleported, they are moved to the right checkpoint, but do not face the next checkpoint, which I want. Also, if you click the last zone, I made it so it faces zone 1 because there is not another zone to face after the last zone, but it gives an error that it tries to face them to the next zone, but it isn’t there. Here is the code:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local WS = game:GetService("Workspace")
local Zones = WS.Zones -- all zones are stored in a folder
script.Parent.Activated:Connect(function()
for _, button in ipairs(script.Parent.Parent.ScrollingFrame:GetChildren()) do
if button:IsA("TextButton") then
button:Destroy() -- this just makes sure that duplicates aren't made; when you close out of the menu, it doesn't add onto the already existing buttons.
end
end
for i = 1, player:WaitForChild("leaderstats"):WaitForChild("Zone").Value do
local button = game:GetService("ReplicatedStorage").Button:Clone()
button.Text = "Zone " .. i
button.Parent = script.Parent.Parent.ScrollingFrame
button.Activated:Connect(function()
local str = button.Text
local num = str:match("%d+") -- checks the name of the button and gets which zone you are attempting to teleport to by using the number in the text.
print(num)
local currentC = num -- checkpoint they want to teleport to
local nextC = num + 1 -- checkpoint they should face
local charPos = char:GetPivot().Position
if currentC == #Zones:GetChildren() then -- if the value they are trying to teleport to is the last zone, it should face them to the first zone, but the code doesn't recognize the if statement, and it doesn't print the 'tttttt' meaning that it skips over this.
print('tttttt')
char:SetPrimaryPartCFrame(CFrame.new(Zones[currentC].Position + Vector3.new(0, 10, 0), Vector3.new((Zones[1].Position).X, charPos.Y, (Zones[1].Position).Z)))
else
char:SetPrimaryPartCFrame(CFrame.new(Zones[currentC].Position + Vector3.new(0, 10, 0), Vector3.new((Zones[nextC].Position).X, charPos.Y, (Zones[nextC].Position).Z))) -- tries to get the next zone if you're trying to click the last zone, but it obviously can't get the position because there is no next zone after the last zone
end
end)
end
end)
So, there are two problems; if you try to click the last zone, it tries to get the next zone, but there is no next zone, so it skips over the if statement I assume. Also, for the other zones, the players are teleported to the correct position, but they do not face the next zone.