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
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
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!
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...