Adding a "Remove Point" command

Hello!

I have been working on a point system that works perfectly in my game and I can add points pretty quickly with a simple command. However, I was wondering how I would go around making a remove point command which deducts the amount of points specified from a player.

Here is the add point script if anybody wants to take a look at it and/or use it.

local GroupId = 10468469 
local MinimumRankToUseCommand = 8 

game.Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
		Player.Chatted:Connect(function(Message)
			local Words = string.split(Message, " ")

			if Words[1] == "!addpoint" then
				local NameOfPlayerToGivePoint = Words[2]
				local AmountOfPointsToGive = Words[3]
				
				local PlayerToGivePoint = game.Players:FindFirstChild(NameOfPlayerToGivePoint)
				
				if PlayerToGivePoint then
					PlayerToGivePoint.leaderstats.Points.Value = PlayerToGivePoint.leaderstats.Points.Value + AmountOfPointsToGive
				end
			end
		end)
	end
end)

Also, I am new to scripting so this is one of the first successful scripts I’ve managed to make (with the help of a few youtube videos describing what each line of code meant aha) so if you see anything I could maybe improve in this script to make it more compact or improve it, please let me know so I can improve!!!

If you want to remove points, can’t you just the same code you used for adding points but instead of using + you use -

Also I’d recommend using a Compound assignment for when you want to add something to itself

a = a + 1

And

a += 1

Would be the same but the 2nd is shorter

And you should lower case the command and then compare

Something like this

local Players = game:GetService("Players")

local GroupId = 10468469 
local MinimumRankToUseCommand = 8 

Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
		Player.Chatted:Connect(function(Message)
			local Words = string.split(Message, " ")
			local Command = Words[1]:lower()

			if Command == "!addpoint" then
				local NameOfPlayerToGivePoint = Words[2]
				local AmountOfPointsToGive = Words[3]
				
				local PlayerToGivePoint = Players:FindFirstChild(NameOfPlayerToGivePoint)
				
				if PlayerToGivePoint then
					PlayerToGivePoint.leaderstats.Points.Value += AmountOfPointsToGive
				end
			elseif Command == "!removepoint" then
				local NameOfPlayerToGivePoint = Words[2]
				local AmountOfPointsToGive = Words[3]
				
				local PlayerToGivePoint = Players:FindFirstChild(NameOfPlayerToGivePoint)
				
				if PlayerToGivePoint then
					PlayerToGivePoint.leaderstats.Points.Value -= AmountOfPointsToGive
				end
			end
		end)
	end
end)

I recommend before this gets bigger you figure out a way to organize the commands, and perhaps add some implementation to get the player regardless of capitalization in their name, and to add some checks to see if the points is a number or not before awarding

3 Likes

Thanks for the reply! As soon as I sent this topic off I thought the same thing after looking back at my script so I should’ve really looked closer anyway. Also, thank you for the tips! They’re really appreciated.

Have a good day. :slight_smile:

1 Like

The addition to the script works great, but all of a sudden every time I add a point the player now moves forwards one or two studs. This could cause problems for things like glitching and sitting in a seat and I have tried looking for what could be the culprit but I can’t seem to find it.

I think its something to do with the script refreshing the character to add the points but I am not to sure.

Does anyone have an idea of what could cause this?

It sounds like an external issue since the code has nothing to cause that, maybe it’s something else being caused by another script?

1 Like

I thought so, thanks for the help! I’ll take a look and try find it.

Also, I decided to remove “AmountOfPointsToGive” and just make it +1 and -1 each time, so thanks for the tips!

1 Like