Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need help finding a player easier- without using i,v in pairs?

I tried to use for i,v in pairs, but my code keeps looping through the players, and every time it does this, it repeats "error"- until it gets to a matching player. I know this is supposed to happen; but is there an easier way?

Commands.warn = function(Sender,Arguments)
	local Username = Arguments[1]
	table.remove(Arguments, 1)
	local Message = table.concat(Arguments, " ")
	if Username == string.lower(Sender.Name) then
		fWarn(Sender,"Error: You can't warn yourself.",true,"rbxassetid://26399276")
	else
	for i,v in pairs(game.Players:GetChildren()) do
			if string.lower(v.Name) == Username then
			fWarn(Sender,"Sucessfully Warned "..v.Name.."!",true,"rbxassetid://377245168")
			fWarn(v,"You have been warned by"..Sender.Name..": "..Message,true,"rbxassetid://377245168")
		else
			fWarn(Sender,"Error.",true,"rbxassetid://26399276")
		end
	end
	end
end

Thanks in advance.

For some context: I'm trying to make it so that when an admin says !warn {Player} {Reason}, that function will fire. I already have the commands working, I just need a better way of finding the player, one that does not involve looping through them all.

1 comment

0

can u accept my answer?

1 answer

✓ Accepted answer

So here is the code. In between the ifs, you add your code. https://scriptinghelpers.org/questions/70527/how-to-stringfind-a-specific-player

local Players = game:GetService("Players")
 
local function onPlayerAdded(player)
	print("Player: " .. player.Name)
	if player.Name = "TheName" then
	--your code here
	end
end
 

for _, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

2 comments

0

not sure what you mean

0

check the link out

This discussion is preserved as it was. New answers, comments, and votes are closed.