Hey there, I am trying to code a chat command, I can not figure out what I am doing/did wrong. Here is the script:
game.Players.PlayerAdded:connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "PlayerValues"
Player.Chatted:connect(function(message)
if message:sub(1, 6) == ":addr " then
local TargetPlayer = game.Players:FindFirstChild(message:sub(7))
if TargetPlayer then
local Character = TargetPlayer.Character
if Character then
Workspace.Leaderboard.Points = Workspace.Leaderboard.Points + 1
end
Maybe you guys can help me, if so please let me know what I am doing wrong and how to fix it…
game.Players.PlayerAdded:connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "PlayerValues"
Player.Chatted:connect(function(message)
if message:sub(1, 6) == ":addr " then
local TargetPlayer = game.Players:FindFirstChild(message:sub(7))
if TargetPlayer then
local Character = TargetPlayer.Character
if Character then
workspace.Leaderboard.Points = workspace.Leaderboard.Points + 1
end
end
end
end)
end)
The code when you say :add player-name stat-name number it should give the player specified the number specified for the specified stat, currently it does not do anything at all…
Your code says the command is :addr and it doesn’t accept any other arguments except player-name.
Anyways, you don’t do anything with TargetPlayer and what is workspace.Leaderboard.Points? Is it a NumberValue? If it is, you should be doing Points.Value.
Is there any errors in the output?
If i were to guess, you should be doing workspace.Leaderboard.Points.Value = Workspace.Leaderboard.Points.Value + 1. Meaning you should be updating a property of an object “Points” whether its Value, Text, blah blah.
That’s not the syntax of the command specified in the script. It doesn’t accept short names, doesn’t allow you to specify the leaderstat, or it’s value. It doesn’t modify the leaderstats.
For starters, instead of using “message:sub” you should be using “message:split(” “)”
Message:split() will return an array of “arguments.”
EG:
local Args = message:split(" ");
if (Args[1] == ":addr" and Args[2] ~= nil and Args[3] ~= nil and Args[4] ~= nil) then
local Player = game:GetService'Players':FindFirstChild(Args[2]);
if (Player) then
Player.leaderstats[Args[3]].Value = Player.leaderstats[Args[3]].Value + tonumber(Args[4]);
end
end
game.Players.PlayerAdded:connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "PlayerValues"
Player.Chatted:connect(function(message)
if message:sub(1, 6) == ":add " then
local Args = message:split(" ");
if (Args[1] == ":add" and Args[2] ~= nil and Args[3] ~= nil and Args[4] ~= nil) then
local Player = game:GetService'Players':FindFirstChild(Args[2]);
if (Player) then
Player.leaderstats[Args[3]].Value = Player.leaderstats[Args[3]].Value + tonumber(Args[4]);
end
end
Remove the if statement for if message:sub(1,6) == ":add " then because the code after that already does it.
I should also say, if Args[3] isn’t a valid leaderstat, or Args[4] isn’t a number, the script will error.
game.Players.PlayerAdded:connect(function(Player)
local Folder = Instance.new("Folder", Player)
Folder.Name = "PlayerValues"
Player.Chatted:connect(function(message)
local Args = message:split(" ");
if (Args[1] == ":add " and Args[2] ~= nil and Args[3] ~= nil and Args[4] ~= nil) then
local Player = game:GetService'Players':FindFirstChild(Args[2]);
if (Player) then
Player.leaderstats[Args[3]].Value = Player.leaderstats[Args[3]].Value + tonumber(Args[4]);
end
end
end)
end)