Need Help with a ChatService Loop

Hi! I’m trying to make a script that gives a particular player a badge when “/award username” is typed in chat. My code is working fine, and I’m able to award badges to particular players, but in the console the chat message keeps getting detected and just infinitely spits out that the player has already relieved that badge.

local groupID = 12775006
local MinRankID = 17
local badgeID = 2128422186
	game.Players.PlayerAdded:Connect(function(plr)
		plr.Chatted:connect(function(msg)
		while true do
			wait()
			if plr:GetRankInGroup(groupID) >= MinRankID then
				if msg:sub(1, 7) == "/award " then
					local PlayerToBeAwarded = game.Players:FindFirstChild(msg:sub(8))
					if PlayerToBeAwarded then
						game:GetService("BadgeService"):AwardBadge(PlayerToBeAwarded.UserId, badgeID)
					end
				end
			end
		end
	end)
end)

Any help is much appreciated!

I don’t understand why you are doing the while true loop. There is no need for it and makes 0 sense for what your trying to do.

If you remove the while true loop it should work if I am not mistaken.

1 Like

This fixed it! Don’t know why I put the loop there, thanks for your help!

1 Like