Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a custom teleporter script?

Asked by
flogau62 -34
6 years ago

Here is the script that teleports to one location on touch, but I need it so when you touched the brick/model 3 times, at the fourth time it needs to teleport to a diffrent location. Can someone do that?

place = CFrame.new(97.689, 3.104, 16.411) script.Parent.Touched:connect(function(p) local humanoid = p.Parent:findFirstChild("Humanoid") if (humanoid ~= nil) then humanoid.Torso.CFrame = place end end)

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Hey flogau62,

For what you're trying to accomplish you just need to do some basic arithmetic and logical programming also, I recommend using tables like I did with mine. Also, make it so your code works under any circumstances like I made mine which I will show you below in a while.

So, what I did in my code is I had a variable for the location, I made a table of all the locations and every time they made a 4th touch, I changed the location to the next one in the table. That's basically what I did. Also, I added 1 to the variable that kept a record of the amount of touches and once those touches reached 4, it would update the location. Below is my code and please, don't just out-right copy my code but, I want you to learn from it. The only one it will demerit if you copy the code and not even try to understand it is you in the long run because, this code is very basic and I wouldn't even care if somebody was to copy it but, if I were you I'd care about myself just copying code and not going the extra mile to have an understanding of it. It will help you in the long run so, thanks for complying. Below is the code and I will explain the code line by line.

local part = script.Parent; -- The part which will be touched to activate teleportation.
local active_location; -- The variable for the current location
local num_touch = 0; -- The variable that keeps track of the amount of touches.
local loc = 1; -- The variable that keeps track of the index of the location in the table.
local deb = false; -- The variable for debounce.

local locations = {CFrame.new(97.689, 3.104, 16.411), CFrame.new(1, 1, 1), CFrame.new(9, 9, 9)} -- Table to store all locations you need.
part.Touched:Connect(function(obj) -- Anonymous function connected to the Touched event
    local hum = obj.Parent:FindFirstChild("Humanoid"); -- Variable for humanoid if there is one.
    if hum and not deb then -- Checks if there is a humanoid and if deb is false.
        deb = true; -- Sets deb to true for debounce purposes
        num_touch = num_touch + 1; -- Increases the number of touches by 1.

        if num_touch == 4 then -- Checks if the amount of touches are 4
            num_touch = 0; -- Sets the amount of touches back to 0.

            if loc == #locations then -- Checks if loc is as great as can be and if it can then it sets loc back to 1. Therefore, once you have ran out of all the CFrames, in the table it goes back to the first one so the code doesn't error saying there isn't the next CFrame.
                loc = 1; -- Sets loc to 1.
            else -- If loc isn't equal to the amount of locations in the table.
                loc = loc + 1; -- Increases loc by 1 since 4 touches have been completed.
            end -- end for loc if statement
        end -- end for num_touch if statement

        active_location = locations[loc]; -- Updates the location to the new/old one.

        local char = obj.Parent; -- Variable for the Character
        local torso = char:WaitForChild("HumanoidRootPart"); -- Variable for the torso

        torso.CFrame = active_location; -- Sets the torso's location to the one in the table.

        wait(1) -- Cool down for 1 second so this function doesn't run twice with 1 touch.
        deb = false; -- Sets debounce to false.
    end -- end for the if hum and not deb if statement.
end) -- en.d for the anonymous function.

Well, I hope I helped and I hope you have a nice day/night.

~~ KingLoneCat

0
Thanks A lot man! :D flogau62 -34 — 6y
0
You spent too much time on this. cabbler 1942 — 6y
0
No problem @flogau62. And, I know @cabbler but if I'm gonna help a person I'd like to help them to the best of my ability. KingLoneCat 2642 — 6y
Ad

Answer this question