Scripting a Stat Edit Command

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…

1 Like

Is that all of the code? If it is, it appears that you left off a few end’s.

Could you provide information as to what your problem is, what the code SHOULD do, what the code does do?

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?

No, it does not show anything…

You don’t provide enough information.
Your code reaches up to workspace.Leaderboard for me, when testing it.

I don’t know what this is supposed to be, so I can’t really help you.
Could you show me the exact command you put into chat?

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 a picture of what I type, here’s a picture of what happens before I do it…

Then here’s a photo of what happens after I run the command…

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.

Could you tell me how to then? I would like to know how to make it accept arguments and find the argument…

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

Like this:

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

I probably got this wrong…

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.

When I run the code it does not work. It shows no error.

Could you send the error? I don’t know what error you’re getting so I can’t help you fix it.

I run:

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)

And I get this:

You can’t use short names. You have to run :add BlueTechnician R 10
The error you’re getting in the output is probably unrelated to the script.

It’s still not working…
image