Badge = Chat Enable

How to make script that disables chat for a player until the player owns a certain badge?

Not that important details

I know its something to do with the marketplaceservice, badgeservice, chatservice and players.
I don’t know much about the chatservice but the docs and tutorials don’t make sense to me.

2 Likes

First you’ll set chat to disabled by default with SetCoreGuiEnabled. Then you’ll want to use UserHasBadgeAsync to check if the player owns the badge or not. Enable chat again if true. But if the player doesn’t own the badge, then make sure to enable the chat when the badge is awarded, so they won’t need to rejoin the game for it to work.

1 Like

These 2 services , functions combinded get you this script

local badgeID = 00000000
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
repeat
wait(0.5)
until game:GetService("BadgeService"):UserHasBadgeAsync(game.Players.LocalPlayer.UserId, badgeID)
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, true)

Looping for an unnecessary purpose is not a great idea. It’s also not good to spam a service as it will timeout. Also task.wait() should be used for all future work.

It would be better to instead only check once. Then to enable chat either then or when the badge is awarded.

well what if they already have badge and they rejoin?

it wouldn’t detect it when you get a badge in game though

Please read through everything before replying. You check first when the player joins, then enable the chat if the player has the badge. Then in whatever script is awarding the badge, the chat can be enabled at that point.

Although, I should’ve pointed out that a RemoteEvent or something similar will be needed to tell the client when to enable the player’s chat.

tested the script there is no timeout in the console
and there is a reason i have a 0.5 delay
and it seems to be working fine only problem is i cant test the badge part because i dont have any badges

There is a point at which it will timeout. The wait only delays it a bit. Also, just because something works doesn’t mean you should be doing it that way. Loops should be used very sparingly, ideally never. This is why we use events instead. In this case, all you need is two points. The point at which the player joins and the point at which a badge is awarded to a player. That means that the execution only happens a minimum of once and a maximum of twice. Where as looping will spam constantly in the background.

When getting into programming, it’s important to realize the difference between just getting something to work and getting something to work efficiently. Computers don’t have unlimited resources. Sure, one loop like this might not cause any issues, but it’s still very bad practice. Also, your test was only with one player. But if there are multiple players, it problem grows exponentially.

In short, please avoid using loops wherever possible. There is almost always a better method.

For rejoining, you can add this bit of code to execute when a new player joins.

game.Players.PlayerAdded:Connect(function(player)
--Add your badge check code here
end)