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

heelp I need to add a prefix?

Asked by 2 years ago
Edited by Ziffixture 2 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).
local player = game.Players.LocalPlayer
local prefix = "."
player.Chatted:Connect(function(msg)
    local target 
    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)
0
Don't call FindFirstChild twice, that's inefficient. Store the return result in a variable, check the result, then use the variable. Ziffixture 6913 — 2y

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

You need to expand on your string pattern.

The prefix you wish to use is considered a "magic character", so you will need to escape the dot to ensure it is evaluated literally. This is done with the % character. Furthermore, you can use string captures to eliminate the need for string:sub.

-- "^" ensures the pattern occurs at the beginning of the string. See the 
-- hyperlink to get a better understanding of the full pattern.

local Arguments = (".goto Ziffixture"):match("^%.goto (.+)")
if Arguments then
    print(Arguments) --> "Ziffixture" (Match was successful)
end
Ad

Answer this question