I have a command that is supposed to add points to a player when it is typed. Here is the command script:
commands.addpoints = function(sender,arguments)
if sender:GetRankInGroup(11635716) >= 7 then
local receiverName = arguments[1]
local receiver = findPlayer(receiverName)
if not receiver then
return nil
end
local leaderstats = receiver:WaitForChild("leaderstats")
local points = leaderstats.Points
local pointsAdded = arguments[2]
points += pointsAdded
end
end
Here is the leaderboard script:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
local rank = Instance.new("StringValue")
rank.Name = "Rank"
rank.Value = player:GetRoleInGroup(11635716)
rank.Parent = leaderstats
end)
For example, if I typed “/addpoints 62penguins 2”, then it should give me 2 points, but when I type that, an error comes up saying “attempt to perform arithmetic (add) on Instance and string” I tried using WaitForChild() to get the Points value of the player, but the error still came up. How do I fix this?
Oh yes, sorry for that. Here is the code that processes each message to see if it is a command and which one it is if it is one:
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message,recipient)
local splitString = message:split(" ")
local slashCommand = splitString[1]
local command = slashCommand:split(prefix)
local commandName = command[2]
print(commandName)
if commands[commandName] then
local arguments = {}
for i = 2, #splitString, 1 do
table.insert(arguments,splitString[i])
end
commands[commandName] (player,arguments)
end
end)
end)
By the way, here is a better way to check for players group rank:
local Group = game:GetService("GroupService")
local groupID = 000000
commands.addpoints = function(sender,arguments)
local Success, Result = pcall(function()
return sender:GetRankInGroup(groupID)
end)
if Success then
if Result >= 7 then
--rest goes here
end
end
end
No, it wouldn’t (or at least shouldn’t). If I typed, “/addpoints 62penguins 2”, then splitString would be /addpoints, 62penguins, 2. After it checks that the command is a command in the game, it will create a table called arguments. Next, it would insert each part of splitString into the table starting at the index of 2. In our example, it would skip /addpoints, and it would only insert 62penguins and 2 into the arguments table. Therefore, arguments[1] is 62penguins, not /addpoints.
local prefix = "/" --what is used to start the command
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(raw_message)
if raw_message.split("")[1] == prefix then --is the first letter the prefix
local args = raw_message.split(prefix)[2].split(" ") --your arguments cutting off the prefix
local command_name = args[1] --the first argument (the name of the command)
if command_name == "addpoints" then --if the command is "addpoints"
local receiverName = args[1] --your code modified to fit my setup
local receiver = findPlayer(receiverName)
if not receiver then return end
local leaderstats = receiver:WaitForChild("leaderstats")
local points = leaderstats.Points
local pointsAdded = args[2]
points += tonumber(pointsAdded)
end
end
end)
end)