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

How do I make a chat command with two players?

Asked by
Loot_O 42
4 years ago
Edited 4 years ago

Alright, so I figured out how to make a command with one player, but what about two? So the command I want to make is :tp. So if you want to know how it works, say like there is two players, Loot_0, and 0_tooL. Then, I say :tp lo 0_ and I get teleported to 0_tooL.

function findPlayer(r)
    for i, v in pairs(game.Players:GetPlayers()) do
        local n = v.Name:lower()
        if n:find(r) then
            return v
        end
    end
end

game.Players.PlayerAdded:Connect(function(plr)
       plr.Chatted:Connect(function(msg)
              if msg:sub(1, 4):lower() == ":tp " then
                     local p = findPlayer(msg:sub(5):lower())
                     local p2 = findPlayer(msg:sub(7):lower())
                     if p and p2 then
                            workspace[p.Name].Head.CFrame = workspace[p2.Name].Head.CFrame
                     end
              end
       end)
end)
--Edit: Focus on lines 13 and 14
--I typed most of this on here, so there might be mistakes.

On line 13, I know it wouldn't work because if I said :tp loot 0_to, it would try to find a player called "loot 0_to". And for line 14, it also wouldn't work cause p would have to be one letter long. So, again, how would I make a chat cmd with two players?

0
Use the HumanoidRootPart and SetPrimaryPartCFrame to move Characters. Your findPlayer function may possibly be having troubles matching the usernames to each player as string.find will locate similar characters within both of them. Ziffixture 6913 — 4y
0
I updated the script so it works much better. Loot_O 42 — 4y

1 answer

Log in to vote
0
Answered by
Raccoonyz 1092 Donator Moderation Voter
4 years ago
Edited 4 years ago

The best option would be string.split.

Read more here: https://developer.roblox.com/en-us/api-reference/lua-docs/string

You will want to split on the space character. You could then transform the command into this:

tp [Player1] [Player2]

That would return a table containing 3 values. You could then reference the 2nd and 3rd value and find the player(s) based on those.

Hope this helped, and good luck on whatever you are trying to make.

If this doesn't work, leave a comment.

0
could you provide me some code? Loot_O 42 — 4y
Ad

Answer this question