Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Chatted that changes a leaderstat isn't working?

Asked by 9 years ago

I'm adding this to my RPG, but when I tested it it aint no working. And I have no idea how to "make it work"

admins = {"Player", "dragonmaster4111"}

game.Players.PlayerAdded:connect(function(plr)
    for i,v in pairs(admins) do
        if v == plr.Name then
            plr.Chatted:connect(function(msg)
                if msg:sub(1,5) == ";gold" then
                    plr.leaderstats.Gold.Value = msg:sub(7)
                end
            end)
        end
    end
end)

ALRIGHT AFTER I'VE TESTED FOR THE FIFTH TIME, IT WORKED!

2 answers

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

Your script works perfectly fine in both Studio Server Test, and Studio Play mode. It could be entirely possible you are running the script in Studio Server Test which identifies players by Player then a number Player1, Player2, Player3 etc.

You might want to add Xetrax suggestion and make sure the value is a number before changing it. I personally use this technique;

if tonumber(msg:sub(7)) ~= nil then --If the string can not be converted to a number, then the function returns nil.
--code here
end
0
I'm testing it online. Operation_Meme 890 — 9y
Ad
Log in to vote
1
Answered by
Xetrax 85
9 years ago

1: If the message isn't 7 characters long, this can throw an error. 2: Instead of the String.sub() method, I would use String.match(msg, "%d*"), which matches and returns any numbers found in the String, 0 or more. (Follow this link to learn more about this kinda stuff: http://wiki.roblox.com/index.php?title=String_patterns#Simple_matching ) 3: Your problem is simple to fix, don't use msg:sub(1, 5), use msg.sub(1, 5), same goes for msg:sub(7)

Hope this helps, if not, someone else is bound to give you the right answer; Regards, Xetrax

0
msg.sub( won't work, THAT will throw a error, the Questioner had that right :sub(. M39a9am3R 3210 — 9y
0
Ah, I believe I had been thinking of String match and not String.sub at the time I wrote this. Xetrax 85 — 9y

Answer this question