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 ! Thanks, WE
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
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)
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