Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you give cash through chat commands?

--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

✓ Accepted 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

2 comments

0

Thanks a lot, I had provided some explanation below the code.

0

a

This discussion is preserved as it was. New answers, comments, and votes are closed.