Jump to content

Simple command - Problems


Sky4D

Recommended Posts

Yeah, i'm having quite a problem here, and I can't figure out how to fix it. I've made this command to have the syntax: /givecash [name] [amount], but it's not working how I thought it would. I've transferred from SA:MP & PAWNO, so this is pretty difficult for me. Any help is appreciated.

function GivePlayerMoney(thePlayer, command, amount)
if(amount <= 500) then
givePlayerMoney(thePlayer, amount)
else if(amount > 500) then
outputChatBox("Invalid amount. (Amount is greater than $500)")
end
end
end
addCommandHandler("givecash", GivePlayerMoney)

Link to comment

try and be more informative with your descriptions, if there isn't an obvious problem its much harder for people to help you when they don't know how its not working

all arguments passed from a command will be strings, so you need to use tonumber() on 'amount' before you can use it as a number

the elseif statement is one word, 'elseif' not 'else if'

thePlayer (first argument passed from a command) is the player that typed the command, so currently your code creates /give and gives money to whoever typed it, not a player that they specify

function GivePlayerMoney(thePlayer, command, targetName, amount)
-- if they didnt specify a target or an amount then return
if not targetName or not amount then return end
 
-- get the player from the name they input
local target = getPlayerFromName(targetName)
-- convert amount to a number
amount = tonumber(amount)
 
-- if the player exists
if target then
-- if the amount is a number
if amount then
if (amount <= 500) then
givePlayerMoney(target, amount)
elseif (amount > 500) then
outputChatBox("Invalid amount. (Amount is greater than $500)",thePlayer)
end
else
outputChatBox("Please specify an amount.",thePlayer)
end
else
outputChatBox("Please specify a valid player name.",thePlayer)
end
end
addCommandHandler("givecash", GivePlayerMoney)

Link to comment
try and be more informative with your descriptions, if there isn't an obvious problem its much harder for people to help you when they don't know how its not working

all arguments passed from a command will be strings, so you need to use tonumber() on 'amount' before you can use it as a number

the elseif statement is one word, 'elseif' not 'else if'

thePlayer (first argument passed from a command) is the player that typed the command, so currently your code creates /give and gives money to whoever typed it, not a player that they specify

function GivePlayerMoney(thePlayer, command, targetName, amount)
-- if they didnt specify a target or an amount then return
if not targetName or not amount then return end
 
-- get the player from the name they input
local target = getPlayerFromName(targetName)
-- convert amount to a number
amount = tonumber(amount)
 
-- if the player exists
if target then
-- if the amount is a number
if amount then
if (amount <= 500) then
givePlayerMoney(target, amount)
elseif (amount > 500) then
outputChatBox("Invalid amount. (Amount is greater than $500)",thePlayer)
end
else
outputChatBox("Please specify an amount.",thePlayer)
end
else
outputChatBox("Please specify a valid player name.",thePlayer)
end
end
addCommandHandler("givecash", GivePlayerMoney)

I'm sorry about my descriptions, i'm not the best at describing via text. :P

I'm testing your code, even though i'm positive it will work.

EDIT: It worked, thank you very much.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...