Possible to make more reliable? If player chats GG

Hello! I created a system that runs whenever the player types gg in the chat! It is supposed be a fun little easter egg inside of the game.

The code

game.Players.PlayerAdded:Connect(function(plr)
	local val = Instance.new("BoolValue", plr)
	val.Name = "gg"
	val.Value = false
	plr.Chatted:Connect(function(msg)
		local loweredMessage = string.lower(msg)
		if loweredMessage == "gg" then
			if val.Value == false then
				val.Value = true
				plr.leaderstats.Points.Value += 100
				game.ReplicatedStorage.BottomMessage:FireClient(plr, "GG! You have earned 100 P$!")
			end
		end
	end)
end)

BottomMessage RemoteEvent is the RemoteEvent that shows a message on the players screen.

If you see some sort of flaw in my code, please let me know :slight_smile: ! Thanks, WE

Edit: Chat is disabled during the race.

1 Like

It looks flawless, but there’s a few things I would do

local val = Instance.new("BoolValue", plr)

I wouldn’t use the 2nd parameter of Instance.new, it’s been known to cause performance drips, especially if you set a lot of properties. To be safe just remove , plr and set the Parent after setting the value

if val.Value == false then

I would honestly opt to use

if not val.Value then

Whilst they do the same thing, typically not (condition) is used

I would maybe change

if loweredMessage == "gg" then
			if val.Value == false then
				val.Value = true
				plr.leaderstats.Points.Value += 100
				game.ReplicatedStorage.BottomMessage:FireClient(plr, "GG! You have earned 100 P$!")
			end
		end

to

if loweredMessage ~= "gg" or val.Value then return end
val.Value = true
plr.leaderstats.Points.Value += 100
game.ReplicatedStorage.BottomMessage:FireClient(plr, "GG! You have earned 100 P$!")

In terms of flaws, I don’t really see anything that could affect anything, the only thing that could flaw something is the call to the RemoteEvent, which is probably just to change a Gui

1 Like

Well you couldve saved the info that the player said with Datastore so the player cant farm it everytime they join (unless the game doesnt save)

And maybe you couldve made it with a table instead (heres how it would look like)

local plrTable = game.Players:GetPlayers()

game.Players.PlayerAdded:Connect(function(player)
       plrTable[player.UserID] = (the datastore getdata) or false
       player.Chatted:Connect(function(msg)
              if string.lower(msg) == "gg" then
                    if not plrTable[player.UserId] then plrTable[player.UserId] = true end
              end
       end)
end)

The rest thats made inside of chatted event can be made in the same way you did.

The thing is, it is not a datastore. This is one time after every race.

Oh then you dont need to put the datastore get data and just straight put the false.

I believe you could still implement his method to not require you to use a BaseValue, something like this + mine

local plrTable = {}

game.Players.PlayerAdded:Connect(function(plr)
	plrTable[plr.UserId] = false
	plr.Chatted:Connect(function(msg)
		local loweredMessage = string.lower(msg)
		if loweredMessage ~= "gg" or plrTable[plr.UserId] then return end
		plrTable[plr.UserId] = true
		plr.leaderstats.Points.Value += 100
		game.ReplicatedStorage.BottomMessage:FireClient(plr, "GG! You have earned 100 P$!")
	end)
end)

Then you’d have to make all the things in the table reset at some point so they can say it again if wanted. This helps make things efficient without using BoolValues

EDIT: I made a mistake with my guard clause, the edited code is the one that should be used

1 Like