So I am trying to make a command bar, and one of the commands is !givecash <plr>, <amo>. Whenever you press the ReturnEnter it should fire the event. Now while there is no problem with that, I do have a problem finding the amount of cash they would like to give the player. It should like something like this in the command bar: !givecash suspectshot108 515000. So my question is how do I find the amount? I know I can find the player with string.sub() and amount with string.sub() and awhile that is working I am lost on a way to get the amount.
LocalScript: Some code is taken out
if(string.sub(CommandLine.Text,1,10)) == string.lower("!givecash ") then local target = game.Players:FindFirstChild(string.sub(CommandLine.Text,11)) local ammount = string.sub(CommandLine.Text,12) print(target,ammount) game:GetService("ReplicatedStorage").AdminEvents["CE"]:FireServer(target) end
you could add up the amount of characters from the command prefix to the player name, so you can use string.sub()
to sub the string in that amount.
local message = "!givecash EXpodo1234ALT 1000" -- lets say this is the message local totalsub = 11 -- 11 since the prefix is 11 characters local players = {"EXpodo1234ALT"} -- maybe just say this was the array that :GetPlayers() returned local subprefix = string.sub(message,totalsub) for i,v in pairs(players) do if string.match(subprefix,v) then -- if it finds the plr name inside the string local amount = string.len(v) -- gets the amount of characters in the name totalsub = totalsub + (amount+1) -- adds it up break -- breaks the loop end end local AmountOfMoney = string.sub(message,totalsub,100) print(AmountOfMoney) -- prints "1000" workspace.NumberValue.Value = workspace.NumberValue.Value + AmountOfMoney -- example
also why I did (amount+1) is because theres a space between the name and amount.
game.Players.PlayerAdded:Connect(function(player) player.Chatted:Connect(function(message) if string.match(message,"!givecash ") then local totalsub = 11 local players = game.Players:GetPlayers() local subprefix = string.sub(message,totalsub) for i,v in pairs(players) do if string.match(subprefix,v.Name) then local amount = string.len(v.Name) totalsub = totalsub + (amount+1) local amountofmoney = string.sub(message,totalsub,100) v.leaderstats.Cash.Value = v.leaderstats.Cash.Value + amountofmoney -- if you had a leaderstats called "Cash" break end end end end) end)
I made a system for trading using the chat with gmatch. I pulled the core of my code and used a textbox as a "CommandLine"
I put an example on my ScriptingHelpers Place(Uncopylocked) Roblox Place
THIS WILL NOT WORK IN A LOCAL SCRIPT You need to setup an event
script.Parent.FocusLost:Connect(function() local words = {} local msg = script.Parent.Text for v in msg:gmatch("[^ ]+") do table.insert(words, v) end if #words == 3 then if words[1] == "!givecash" then if game.Players:FindFirstChild(words[2]) and tonumber(words[3]) then game.Players[words[2]].Amount.Value = game.Players[words[2]].Amount.Value + words[3] script.Parent.Text = "" end end end end)