What do you want to achieve?
I’d like to create a cash command that is like !cash [user] [amount] that would proceed to give the specified user the specified amount.
What is the issue?
The chat command doesn’t work. No errors appear and no values are changed. No values are printed to the console when I use print and try to print the arguments out. No errors appear either so it’s very confusing to me!
Script (INSIDE A NORMAL SCRIPT IN ServerScriptService)
local ChatService = game:GetService("TextChatService")
local cashCommand = "!cash"
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if string.sub(msg, 1, #cashCommand + 1) == cashCommand then
local args = msg:sub(#cashCommand + 1):split(" ")
if #args == 2 then
local targetPlayerName = args[1]
local amount = tonumber(args[2])
if targetPlayerName and amount then
local targetPlayer = game.Players:FindFirstChild(targetPlayerName)
if targetPlayer then
targetPlayer.Buxs.Value += args[2]
end
end
end
end
end)
end)
If there is any other information I can provide then please let me know!
This did work. I put a print statment right before the check if there are 2 args and one right after the code if #args == 2 then. The print statement that came before the check appeared but the one right below the if statement didn’t.
Updated code if you do not understand what I’m explaining here.
local ChatService = game:GetService("TextChatService")
local cashCommand = "!cash"
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if string.sub(msg, 1, #cashCommand) == cashCommand then
print("Check #1")
local args = msg:sub(#cashCommand + 1):split(" ")
if #args == 2 then
print("Check #2")
local targetPlayerName = args[1]
local amount = tonumber(args[2])
if targetPlayerName and amount then
local targetPlayer = game.Players:FindFirstChild(targetPlayerName)
if targetPlayer then
targetPlayer.Buxs.Value += args[2]
end
end
end
end
end)
end)
local cashCommand = "!cash"
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
local args = string.split(msg, " ")
local command = table.remove(args, 1)
if command ~= cashCommand then return end -- not the right command.
if #args < 2 then return end -- not enough arguments.
local targetPlayer = game.Players:FindFirstChild(args[1])
local amount = tonumber(args[2])
if not targetPlayer then return end -- failed to get player.
if not amount then return end -- amount isn't a valid number.
targetPlayer.Buxs.Value += amount
end)
end)
“Here’s the ready solution, and I also recommend you to create a TextCommand in TextChatService.TextChatCommands. Through this command, it would be much easier to connect all this through the player’s local script + this command wouldn’t stay in the chat.”
local ChatService = game:GetService("TextChatService")
local cashCommand = "!cash"
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
local Splt = msg:split(" ")
if table.find(Splt,cashCommand) then
local NumCashCom = table.find(Splt,cashCommand)
local NamePlrCash = Splt[NumCashCom + 1]
local ValueCash = Splt[NumCashCom + 2]
print(NamePlrCash)
print(ValueCash)
local targetPlayer = game.Players:FindFirstChild(NamePlrCash)
if targetPlayer then
local NumbValue = tonumber(ValueCash)
if NumbValue ~= nil then
targetPlayer.Buxs.Value += NumbValue
end
end
end
end)
end)