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

How do you give cash through chat commands?

Asked by 3 years ago
--Scripted by Electrowizard12312--
local Players = game:GetService("Players")
local cashCommand = "/give "
--
local function onOwnerChatted(player, message)
    if message:sub(1, cashCommand:len()):lower() == cashCommand:lower() then
        local name = message:sub(cashCommand:len() + 1)
        local player = Players:FindFirstChild(name)
        local amount = message:sub(cashCommand:len()+ name + 1 )
        if player then
            player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + amount
            print("The creator gave "..name.." "..amount.." cash.")
        else
           print("Couldn't find player!")
        end
    end
end
--
local function onPlayerAdded(player)
    if player.UserId == 586053124 then
        player.Chatted:Connect(function (...)
            onOwnerChatted(player, ...)
        end)
    end
end
--
for _, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- Scripted by Electrowizard12312

This script is supposed to give cash to a player when someone chats /give player amount, but for some reason, it is considering local amount as nil(line 9) and it doesn't give any cash(No output). But if I make line 9 as 1000 or any value, it works.

How do I fix this?

1 answer

Log in to vote
0
Answered by 3 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
local function onOwnerChatted(player, message)
    message=message:lower()
    local cmd,arg1,arg2=unpack(message:split(" "))
    if cmd~="/give" or not arg1 or not arg2 then return end
    arg2=tonumber(arg2)
    if not arg2 then return end
    local target
    for _,v in ipairs(Players:GetPlayers())do
        if v.Name:lower():sub(1,arg1:len())==arg1 then
            target=v
            break
        end
    end
    if target then
        target.leaderstats.Cash.Value+=arg2
    end

end
0
Thanks a lot, I had provided some explanation below the code. Electrovio 2 — 3y
0
a 4KChanxxe 4 — 3y
Ad

Answer this question