Add Minute Command (Leaderstats)

Greetings Developers,

I don’t have that much experience with scripting and I would like to ask if its possible to do a command which can add and remove minutes from a user related to the leaderstats.
As a example “!addminute vChillq 20” or “!removeminute vChillq 20”.

Here is the script and a image about the minute leaderstats:


Screenshot 2022-06-17 182812

2 Likes

It is possible, we can get the message when the player chats with the Player.Chatted event.

game.Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        --//This is the part where you do the command
  end)
end)

To get the command, player and variable we will split the message. For this, we can use string.split(), it returns a table of strings that we get after splitting it. We should also make the message lowercase so we can get the same result when the player writes it uppercase.

local arguments = string.split(string.lower(msg)," ") --//This is a table containing all the keywords
local command = arguments[1]
local user = arguments[2]
local amount = arguments[3]

I think you’d be able to do the rest from here. You just need to check if these values are valid and if the command is !addminute or !removeminute. After that, you can add the minute or remove it.

1 Like

This is complicated for me I don’t understand anything.

Okay so, as @Hexlinee said. We will first use the .Chatted event. And split up the message into the commands and the player (that we remove stuff from), so we can have to same “grammar” that you used in your example command. (We also validate our commands/keywords to make sure they exist using some guard clauses)

local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        local arguments = string.split(string.lower(msg)," ") --//This is a table containing all the keywords
        local command = arguments[1]
        local user = arguments[2]
        local amount = arguments[3]

        if not command or not user or not amount or not tonumber(amount) then return end 
        local player = Players:FindFirstChild(user)
        if not player then return end
  end)
end)

Next, we check what the requested command was and do something based on that, in your case we look for the “!removeminute” and “!addminute” commands, and then either add or subtract from the players minutes by the desired amount.

local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        local arguments = string.split(string.lower(msg)," ") --//This is a table containing all the keywords
        local command = arguments[1]
        local user = arguments[2]
        local amount = arguments[3]

        if not command or not user or not amount or not tonumber(amount) then return end 
        local player = Players:FindFirstChild(user)
        if not player then return end
        local minutes = player:WaitForChild("leaderstats"):WaitForChild("Minutes")
        if command == "!removeminute" then
            minutes.Value -= amount
        elseif command == "!addminute" then
            minutes.Value += amount
        end
  end)
end)
1 Like

Can every Player use it or only the Owner and does it also save? I forgot it to said it.

Rn everyone can use it, but you can easily change it with 2 lines:
local admins = {336965291}
and
if not table.find(admins, player.UserId) then return end

So the code should look like this now:

local Players = game:GetService("Players")
local admins = {336965291}

Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        if not table.find(admins, player.UserId) then return end
        local arguments = string.split(string.lower(msg)," ") --//This is a table containing all the keywords
        local command = arguments[1]
        local user = arguments[2]
        local amount = arguments[3]

        if not command or not user or not amount or not tonumber(amount) then return end 
        local player = Players:FindFirstChild(user)
        if not player then return end
        local minutes = player:WaitForChild("leaderstats"):WaitForChild("Minutes")
        if command == "!removeminute" then
            minutes.Value -= amount
        elseif command == "!addminute" then
            minutes.Value += amount
        end
  end)
end)

Hi! I imagine it would look something a bit like this…

(I have included a function that allows the Command Caller to not have to say the SubjectPlayer’s full username).

--in a regular server script
game.Players.PlayerAdded:Connect(function(cCaller)
	
	local function getPlr(givenstring)
		for _,plr in pairs(game.Players:GetPlayers()) do
			if plr:IsA("Player") then
				local plrsname = plr.Name
				if plrsname:lower():find(givenstring) then
					return plr
				end
			end
		end
	end
	
	cCaller.Chatted:Connect(function(Command)
		local Arguments = string.split(Command, " ")
		if Arguments[1]:find("!addpoints") then
			local SubjectPlayer = getPlr(Arguments[2])
			
			if SubjectPlayer ~= nil then
				local amountToGive = tonumber(Arguments[3])	
				
				local lstsFold = SubjectPlayer:FindFirstChild("leaderstats")
				local lstsVal = lstsFold:FindFirstChild("Minutes")
				
				lstsVal.Value += amountToGive
			else
				--i would reccomend putting a little error message here to show up on the comamndcaller's playergui 
				-- something like.. "player could not be found in game"
			end
		elseif Arguments[1]:find("!takepoints") then
			local SubjectPlayer = getPlr(Arguments[2])

			if SubjectPlayer ~= nil then
				local amountToGive = tonumber(Arguments[3])	

				local lstsFold = SubjectPlayer:FindFirstChild("leaderstats")
				local lstsVal = lstsFold:FindFirstChild("Minutes")

				lstsVal.Value -= amountToGive
			else
				--i would reccomend putting a little error message here to show up on the comamndcaller's playergui
				-- something like.. "player could not be found in game"
			end
		end
	end)
end)

Hope this helps :slightly_smiling_face:

Seems not working. It doesn’t take or gives minutes to a player. Is it combined with the script on the image the I sent?

It should work, gimme a sec to test it. I wrote that code on mobile so it was untested lol.

Ok well I can’t go on my computer right now so, can you debug it?

Add prints above:

And

And add a print for this

Welp I tested it on Roblox Studio and it doesn’t add or remove minutes from a player.

Did you add the prints like I asked, if so what did it output @vChillq

I dont understand what you mean with prints. So im basically just copying the script and put it in “ServerScriptService”

Try this script, and tell me what prints you get in the output.


local Players = game:GetService("Players")
local admins = {336965291}

Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        if not table.find(admins, player.UserId) then return end
        local arguments = string.split(msg," ") --//This is a table containing all the keywords
        local command = arguments[1]
        local user = arguments[2]
        local amount = arguments[3]

        print("Before first check,", command, user, amount)
        if not command or not user or not amount or not tonumber(amount) then return end 
        local player = Players:FindFirstChild(user)
        print("Findint Player")
        if not player then return end
        print("Found Player")
        local minutes = player:WaitForChild("leaderstats"):WaitForChild("Minutes")
        print(minutes)
        if command == "!removeminute" then
            minutes.Value -= amount
        elseif command == "!addminute" then
            minutes.Value += amount
        end
  end)
end)

@vChillq

These are the prints I got in the output:

21:05:04.522 Before first check, !addminute vchillq 100 - Server - Minutes Command:12
21:05:04.522 Findint Player - Server - Minutes Command:15
21:07:08.184 Before first check, !removeminute vchillq 100 - Server - Minutes Command:12
21:07:08.184 Findint Player - Server - Minutes Command:15

The commands didn’t gave / removed minutes from a player.

Ok, so what did you type in the chat to get these results, what is happening is it’s not finding the amount of minutes you want to add/remove

@vChillq

I typed in chat “!addminute vChillq 100” and “!removeminute vChillq 100”

Try changing this line:

local arguments = string.split(string.lower(msg)," ")

On this:

local arguments = string.split(msg," ")

Otherwise you can do this:

local function get_lower_name(text)
	local players = Players:GetPlayers()
	
	for i in pairs(players) do
		if not string.find(players[i].Name:lower(), string.lower(text)) then
			table.remove(players, players[i])
		end
	end
	return players
end

And then:

local user = get_lower_name(arguments[2])[1]

Yeah, I fixed the issue. I didn’t realize that was there because I was going off what the first person put, so that I didn’t have to re explain all of that. Anyways. the other script is edited, or it’s here:

local Players = game:GetService("Players")
local admins = {336965291}

Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        if not table.find(admins, player.UserId) then return end
        local arguments = string.split(msg," ") --//This is a table containing all the keywords
        local command = arguments[1]
        local user = arguments[2]
        local amount = arguments[3]

        print("Before first check,", command, user, amount)
        if not command or not user or not amount or not tonumber(amount) then return end 
        local player = Players:FindFirstChild(user)
        print("Findint Player")
        if not player then return end
        print("Found Player")
        local minutes = player:WaitForChild("leaderstats"):WaitForChild("Minutes")
        print(minutes)
        if command == "!removeminute" then
            minutes.Value -= amount
        elseif command == "!addminute" then
            minutes.Value += amount
        end
  end)
end)
1 Like

I reccomend you add this function into your code, this allows the commandcaller to not have to type the correct capitals, and not the full name.

Replace the

local player = Players:FindFirstChild(User)

With,

local player = getPlr(user)

Here is the given function you will need… Hope this makes this a bit more convenient!

local function getPlr(givenstring)
		for _,plr in pairs(game.Players:GetPlayers()) do
			if plr:IsA("Player") then
				local plrsname = plr.Name
				if plrsname:lower():find(givenstring) then
					return plr
				end
			end
		end
	end

Remember, it may not find your player so I reccomend you do;

if player ~= nil then
   --commmand function goes here
end
1 Like

So how would then you’re full script look like?

1 Like