xxldoener Posted June 2, 2012 Share Posted June 2, 2012 Ok, I got a couple of problems with my script function sendmoney (source, commandname, target, ammount) local sourcemoney = getPlayerMoney (source) local sourcename = getPlayerName ( source ) --if sourcemoney >= ammount then takePlayerMoney (source, ammount) givePlayerMoney (target, ammount) outputChatBox ( "You gave " .. target .. " " .. ammount .. "$", source) outputChatBox ( "" .. sourcename .. "gave you " .. ammount .. "$", target ) --else --outputChatBox ( "You do not have enough money", source ) --end end addCommandHandler("sendmoney", sendmoney) 1. The target player doesn't get any money 2. The sender also sees the message that only the target should see 3. If I want to include Line 4 I get an error that a string is compared to a number What am I doing wrong? Link to comment
Castillo Posted June 2, 2012 Share Posted June 2, 2012 1: You should convert the "amount" argument to a number with the function: tonumber ( string ). 2: Target is just a string there, you must get the player element from it's name with the function: getPlayerFromName. Link to comment
xxldoener Posted June 2, 2012 Author Share Posted June 2, 2012 A couple more question since you are so good at answering them How can I check if the player is online on the server, so the script only sends money if the player is online. And would it also be possible to transfer money to another player if he is offline And how do I save the position, skinid, money, wantedlevel, weapons, ... of a player when he goes offline? Edit: I think I solved the first question. My code right now: function sendmoney (source, commandname, target, ammount) local sourcemoney = getPlayerMoney (source) local sourcename = getPlayerName ( source ) local targetp = getPlayerFromName (target) if targetp then if sourcemoney >= tonumber (ammount) then takePlayerMoney (source, ammount) givePlayerMoney (targetp, ammount) outputChatBox ( "You gave " .. target .. " " .. ammount .. "$", source) outputChatBox ( "" .. sourcename .. "gave you " .. ammount .. "$", targetp ) else outputChatBox ( "You do not have enough money", source ) end else outputChatBox ( "Invalid target specified", source ) end end addCommandHandler("sendmoney", sendmoney) Link to comment
myonlake Posted June 2, 2012 Share Posted June 2, 2012 function findPlayer(name) local matches = {} for i,v in ipairs(getElementsByType("player")) do if getPlayerName(v) == name then return v end local playerName = getPlayerName(v):gsub("#%x%x%x%x%x%x", "") playerName = playerName:lower() if playerName:find(name:lower(), 0) then table.insert(matches, v) end end if #matches == 1 then return matches[1] end return false end addCommandHandler("sendmoney", function(player, cmd, name, amount) local amount = tonumber(amount) if name and amount then local target = findPlayer(name) local money = getPlayerMoney(target) if money >= amount then takePlayerMoney(player, amount) givePlayerMoney(target, amount) outputChatBox("You gave " .. getPlayerName(target) .. " $" .. amount .. ".", player, 0, 255, 0, false) outputChatBox(getPlayerName(player) .. " gave you $" .. amount .. ".", target, 0, 255, 0, false) else outputChatBox("You do not have enough money.", target, 255, 0, 0, false) end end end ) Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now