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

How do i make this teleport me to any player?

Asked by 5 years ago
Edited 5 years ago

what is it in this script that i need for it to look for what i typed inside players and teleport me to that players head this is a local script inside startertool script:

local player = game.Players.LocalPlayer
player.Chatted:Connect(function(msg)
if msg:lower() == "goto "..game.Players--)something in between here but i dont know what) then
    player.Character.HumanoidRootPart.CFrame = CFrame.new(.Character.Head.Position)
end
end)

the idea is for me to have an admin tp command so if i would say "goto slayer" then i would go to slayer without putting in slayer in the script

0
what is wrong with my answer? starmaq 1290 — 5y
0
u give me a script that works in a completely other way even though i asked help to teleport to any player i type, with ur scrpt its to a player i type in the script and i already got that type Gameplayer365247v2 1055 — 5y
0
that is completley wrong... did you even try it? just open a server with as many players as you want, and type the command followed by a Player's name, and you'll teleport to him, i tested this, opened a server with 4 people, and it worked. starmaq 1290 — 5y
0
i did try, otherwise i wouldnt have said that. but fine ill try again Gameplayer365247v2 1055 — 5y
View all comments (9 more)
0
... starmaq 1290 — 5y
0
wanna help me test it, our game is still not kinda released https://www.roblox.com/games/2965484894/testing-stage Gameplayer365247v2 1055 — 5y
0
its private i think starmaq 1290 — 5y
0
no its public Gameplayer365247v2 1055 — 5y
0
i think only friends of yours can join starmaq 1290 — 5y
0
1. its a public game, anyone can join. 2 i tested with my alt on my phone and it didnt work, as i said. the error was a nil value https://imgur.com/a/XxJF2L4 Gameplayer365247v2 1055 — 5y
0
hay, try it out now, i did fix this in my test place last night, but i didnt edit my answer, it should 100% work now starmaq 1290 — 5y
0
check it out starmaq 1290 — 5y
0
FINALY works now Gameplayer365247v2 1055 — 5y

2 answers

Log in to vote
3
Answered by
starmaq 1290 Moderation Voter
5 years ago
Edited 5 years ago

Ok so, first you would need to know the player that you want to teleport to. We would simply use a string function called string.sub. There a lot and a lot of dtring fnuctions, string manupilation is really cool and useful. Check it out. What string.sub does is, it gives a porition of the given string, it cuts the string starting from a character we can choose and ending at another character we can also choose.

print(string.sub("i like apples", 3, 10))
--you see, this should print "like app", because we told it to cut the string starting from the 3rd character which "l" and ending from the 10th character, which is the second "p" in apple. And that's it. And btw, if we only set the 2nd argument which is the start and we didnt set the 3rd which is the end, it will start from the start we gave it as usual, but it will sub it until the string's end.
print(string.sub("i like apples", 3))
--this will print "like apples", because we didnt set an end, and it went all the way to the last character

And since we know where the player name is going to start from, it's always going to start from the 6th character, so that should be easy. Another thing, string functions can be found in 2 ways; you can do them this way string.sub() or more simpler a string:sub() We can now get the player that was written in the command msg, but what about the teleportation.

local player = game.Players.LocalPlayer
player.Chatted:Connect(function(msg)
    local target --this would be the player that we're gonna tp to, its just a varibale to make things cleaner.
    if msg:lower():match("goto") then
        if game.Players:FindFirstChild(msg:sub(6)) then
            target = workspace:FindFirstChild(msg:sub(6))
            print(target)
            player.Character.HumanoidRootPart.CFrame = CFrame.new(target.Head.Position)
        end
    end
end)

And that should work! This was tested and it fully work

!Works

Now, let me explain some stuff.

--this part
msg:lower():match("goto")
-- :match is a cool function too, i used it here to check if the word "goto" was inside that message. (and since if statments will pass through whenever the condition is true or a non-nil value, which means any values that's not nil, so pretty much strings, integers...), and :match returns a string, a string containing the words that much together, so for example.

print(string.match("i like bloxy cola", "bloxy cola"))
--this should return "bloxy cola", since its found in the 2 strings, and thats what i did, i searched if "goto" was written to make it so the command work.

This part doesn't need explaining but i'll show it to you.

 if game.Players:FindFirstChild(msg:sub(6)) then

--now after we confirmed that the command is right, we check if there was a player given, if the message subbed starting from 6 (thats where the player's name starts), which returns a string containing the player's name; we checked if that player exists inside game.Players; and taht's really it

Happy to help!

1
Nice use of string manipulation! Keep up the good work! SmhMate 4 — 5y
0
wow, tahnks a lot :> ! starmaq 1290 — 5y
0
this isnt what i wanted, i want to be able to type any players name and teleport to them, i already got a much easier script for a set player Gameplayer365247v2 1055 — 5y
0
:thonk: starmaq 1290 — 5y
View all comments (3 more)
0
that's exactly what i did starmaq 1290 — 5y
0
how is it now what you want? starmaq 1290 — 5y
0
@Gameplayer365247v2 Stay quiet. You clearly didn't fully read his answer. DeceptiveCaster 3761 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Try this.

local player = game.Players.LocalPlayer
player.Chatted:Connect(function(msg)
if msg:lower() == "goto "..game.Players:lower() then --make sure to only have comments at the end(:
    player.Character.HumanoidRootPart.CFrame = CFrame.new(.Character.Head.Position)
end
end)

Because you have a comment in the middle, the rest is considered a comment.

print("hi "--(:--)

See how the last bracket is green. That means it's a comment. also, if you type goto Roblox, then it sees it as "goto roblox" which then is not goto Roblox, because CaPiTaLs matter. by adding :lower() they are both lowercase, so it treats ROBLOX andRobloX the same.

bear in mind i am terrible at scripting, so mine could be even worse! comment if it worked, if not i am not sure...

0
That is really wrong, there a couple of rookie mistakes taht i'm sure you'll fix! and i do recommend you answer others's questions, but always answer a question you would know the answer for, learn more and you'll be able to answer anything! starmaq 1290 — 5y

Answer this question