How do I detect if the first words of a string are a pattern

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    So I am making a admin and I want it so you only need to type the first words of a player to run the command on him

  2. What is the issue? Include screenshots / videos if possible!
    I Don’t know how

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Search And tried to use :find() but it doesn’t detect if the first words are like the pattern it just detects if it found the pattern in a string

I don’t speak English very good so I hope you know what I meant

You could combine string:match alongside string:lower.

An example:

for _,Player in pairs(Players:GetPlayers()) do
    if Player.Name:match(Target) then
         --code
    end
end
--Target here is the person whom the action is being executed on
1 Like

I tried it but didn’t work

If someone name was “Test123”
And I type “123” it will detect it but I want so I need to type the first words of it to work

Change the match to ``^ … Target", this will require it to match the start. String Patterns (roblox.com)

Am new to scripting can you please put it in a script?

Modifying @Valkyrop’s script gives us this:

for _,Player in pairs(Players:GetPlayers()) do
    if Player.Name:match("^" .. Target) then
         --code
    end
end
1 Like

It didn’t work And if I put random words it still says the player was found

@ifkpop is correct, can you share your version of the code?

1 Like

It looks something like this

for _,TargetPlayer in pairs(game.Players:GetPlayers()) do
	if TargetPlayer.Name:match("^" ..PlayerName) then
		Run(TargetPlayer)
	end
end

What does it look like exactly?

Because you have PlayerName there instead of TargetPlayer.Name that was wrong never mind

PlayerName is likely the search query. I would like to see the Players list and PlayerName if that is possible.

1 Like

This is the script its inside a textbutton

local Button = script.Parent
local PlayerName = Button.Parent.Player.Text

function Run(TargetPlayer,Speed)
	Button.Run_Event:FireServer(TargetPlayer,Speed)
end

Button.MouseButton1Click:Connect(function()
	if string.lower(PlayerName) == "me" then
		local TargetPlayer = game.Players.LocalPlayer
		Run(TargetPlayer,tonumber(Button.Parent.Speed.Text))
	elseif string.lower(PlayerName) == "all" then
		for _, TargetPlayer in pairs(game.Players:GetChildren()) do
			if TargetPlayer:IsA("Player") then
				Run(TargetPlayer,tonumber(Button.Parent.Speed.Text))
			end
		end
	elseif string.lower(PlayerName) == "others" then
		for _, TargetPlayer in pairs(game.Players:GetChildren()) do
			if TargetPlayer:IsA("Player") and TargetPlayer.Name ~= game.Players.LocalPlayer.Name then
				Run(TargetPlayer,tonumber(Button.Parent.Speed.Text))
			end
		end
	else
		for _,TargetPlayer in pairs(game.Players:GetPlayers()) do
			if TargetPlayer.Name:match("^" ..PlayerName) then --Error
				Run(TargetPlayer,tonumber(Button.Parent.Speed.Text))
			end
		end
	end
end)

You can simplify this code slightly:

PlayerName = PlayerName:lower()

local targets = { game.Players.LocalPlayer }

if PlayerName == "all" or PlayerName == "others" then
	targets = game.Players:GetPlayers()
	if PlayerName == "others" then
		table.remove(targets, table.find(game.Players.LocalPlayer)
	end
else
	for _, TargetPlayer in pairs(game.Players:GetPlayers()) do
		if TargetPlayer.Name:match("^" ..PlayerName) then
			table.insert(targets, TargetPlayer)
		end
	end
end

for _, target in pairs(targets) do
	Run(target,tonumber(Button.Parent.Speed.Text))
end

Edit: This

May also be written as:

else
	for _, TargetPlayer in pairs(game.Players:GetPlayers()) do
		if TargetPlayer.Name:match("^" ..PlayerName) then
			Run(TargetPlayer,tonumber(Button.Parent.Speed.Text))
		end
	end
	return
end

Nvm am stupid I put the PlayerName variable outside the function Now it works Thank you very much I spent days trying to fix this problem

2 Likes

Thank you I have already found the problem

I put the PlayerName variable outside the function so the PlayerName variable had nothing stored in it

I recommend using the debugger next time!

It lets you step through your code line-by-line and see what the variables are as you go.

1 Like

Ain’t no way I am learning of a debugger now.

I’ve always cursed Roblox Studio for not having it, but I just didn’t know it existed! Thanks a lot for letting me know! You have just save me hours of debugging!

1 Like

Thank you for helping me too

This problem toke days the fix

and thanks for telling me a way to debug