koragg Posted October 24, 2016 Share Posted October 24, 2016 (edited) Hey everyone, So basically the full command is /give (part of) nick amount. That way players can give coins to each other but there is one thing that I'm not able to fix myself. When a player enters just /give (part of) nick without the amount part, a chatbox message is shown stating the full command. When a player writes a nickname which isn't on the server, it says invalid user. Now here comes my problem, when a player enters just /give without anything else after it, I want it to show the same message I mentioned above which shows the correct command's syntax in chat. At the moment if you type /give it shows few errors in debug (that it can't find part of the name, in GetPlayerFromPartialName function). I need it to show a message telling the full command to the user who entered /give and ofcourse there should be no debug errors. I tried to figure it out by using [arg] but no luck, and I'm not really familiar with how that works, even though I have an example script which uses arguments and also requires 3 of them to work. function giveCash(player, command, who, amount) local playeraccount = getPlayerAccount (player) local otherguy = getPlayerFromPartialName(who) or getPlayerFromName(who) if isGuestAccount(playeraccount) then outputChatBox("You need to register and login in order to share coins!", player, 255,153,0) return end if not amount then outputChatBox("#FF0000Correct command is : #FEFE22/give [(part of) nick] [amount]", player, 0, 255, 0, true) return end if not otherguy then outputChatBox("No player found.", player, 255, 0,0) return end if otherguy == player then outputChatBox("In case you haven't noticed, you're trying to give KnightCoins to yourself. #FEFE22WAKE UP!", player, 0, 255, 0, true) return end coinslimit = tonumber(getAccountData(playeraccount, "knightcoins") or 0) if round(tonumber(amount)) > coinslimit then outputChatBox("You don't have so many KnightCoins. Please select a lower amount to give.", player, 255, 0, 0, true) return end if otherguy then local playeraccount = getPlayerAccount (player) local otheraccount = getPlayerAccount (otherguy) if isGuestAccount(otheraccount) then outputChatBox("This player is not registered / logged in.", player, 255, 0, 0, true) return end givercash = tonumber(getAccountData(playeraccount, "knightcoins") or 0) - round(tonumber(amount)) setAccountData (playeraccount, "knightcoins", givercash) setElementData (player,"data.knightcoins", givercash, true) if round(amount) ~= 1 then outputChatBox("#00FFFFYou gave #00FF00"..comma_value(round(tonumber(amount))).." #00FFFFKnightCoins to #FFFFFF"..getPlayerName(otherguy).."#00FFFF.", player, 0, 255, 255, true) recievercash = tonumber(getAccountData(otheraccount, "knightcoins") or 0) + round(tonumber(amount)) setAccountData (otheraccount, "knightcoins", recievercash) setElementData (otherguy,"data.knightcoins", recievercash, true) else outputChatBox("#00FFFFYou gave #00FF00"..comma_value(round(tonumber(amount))).." #00FFFFKnightCoin to #FFFFFF"..getPlayerName(otherguy).."#00FFFF.", player, 0, 255, 255, true) recievercash = tonumber(getAccountData(otheraccount, "knightcoins") or 0) + round(tonumber(amount)) setAccountData (otheraccount, "knightcoins", recievercash) setElementData (otherguy,"data.knightcoins", recievercash, true) end if round(amount) ~= 1 then outputChatBox("#FFFFFF"..getPlayerName(player).." #00FFFF gave you #00FF00"..comma_value(round(tonumber(amount))).." #00FFFFKnightCoins.", otherguy, 0, 255, 255, true) else outputChatBox("#FFFFFF"..getPlayerName(player).." #00FFFF gave you #00FF00"..comma_value(round(tonumber(amount))).." #00FFFFKnightCoin.", otherguy, 0, 255, 255, true) end end end addCommandHandler("give", giveCash) -------------------------------------------------------------------------------------------------------------------------- --http://lua-users.org/wiki/FormattingNumbers function comma_value(amount) local formatted = amount while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if (k==0) then break end end return formatted end -------------------------------------------------------------------------------------------------------------------------- function getPlayerFromPartialName(thePlayerName) local thePlayer = getPlayerFromName(thePlayerName) if thePlayer then return thePlayer end for _,thePlayer in ipairs(getElementsByType("player")) do if string.find(string.gsub(getPlayerName(thePlayer):lower(),"#%x%x%x%x%x%x", ""), thePlayerName:lower(), 1, true) then return thePlayer end end return false end -------------------------------------------------------------------------------------------------------------------------- --http://lua-users.org/wiki/FormattingNumbers function round(val, decimal) if (decimal) then return math.floor( (val * 10^decimal) + 0.5) / (10^decimal) else return math.floor(val+0.5) end end Edited October 24, 2016 by koragg Link to comment
Gravestone Posted October 24, 2016 Share Posted October 24, 2016 You can try if who ~= "" then This will check if the player left the second argument of the command /give empty. Link to comment
koragg Posted October 24, 2016 Author Share Posted October 24, 2016 (edited) attemp to index local 'thePlayerName' (a nil value), line 361. That's the getfrompartialname function there. Edited October 24, 2016 by koragg I actually get the same error^ when I didn't use what u suggested. Link to comment
Walid Posted October 24, 2016 Share Posted October 24, 2016 Try this function giveCash(player, command, who, amount) local playeraccount = getPlayerAccount (player) if playeraccount and not isGuestAccount(playeraccount) then if who then local otherguy = getPlayerFromPartialName(who) if otherguy then if otherguy ~= player then local otheraccount = getPlayerAccount (otherguy) local coinslimit = tonumber(getAccountData(playeraccount, "knightcoins") or 0) if otheraccount and not isGuestAccount(otheraccount) then if amount and tonumber(amount) then if round(tonumber(amount)) > coinslimit then local givercash = coinslimit - round(tonumber(amount)) local recievercash = tonumber(getAccountData(otheraccount, "knightcoins") or 0) + round(tonumber(amount)) local plu = (round(tonumber(amount)) ~= 1 and "s") or "" setAccountData (playeraccount, "knightcoins", givercash) setElementData (player,"data.knightcoins", givercash, true) setAccountData (otheraccount, "knightcoins", recievercash) setElementData (otherguy,"data.knightcoins", recievercash, true) outputChatBox("#00FFFFYou gave #00FF00"..comma_value(round(tonumber(amount))).." #00FFFFKnightCoin"..plu.." to #FFFFFF"..getPlayerName(otherguy).."#00FFFF.", player, 0, 255, 255, true) outputChatBox("#FFFFFF"..getPlayerName(player).." #00FFFF gave you #00FF00"..comma_value(round(tonumber(amount))).." #00FFFFKnightCoin"..plu..".", otherguy, 0, 255, 255, true) else outputChatBox("You don't have so many KnightCoins. Please select a lower amount to give.", player, 255, 0, 0, true) end else outputChatBox("#FF0000Correct command is : #FEFE22/give [(part of) nick] [amount]", player, 0, 255, 0, true) end else outputChatBox("This player is not registered / logged in.", player, 255, 0, 0, true) end else outputChatBox("In case you haven't noticed, you're trying to give KnightCoins to yourself. #FEFE22WAKE UP!", player, 0, 255, 0, true) end else outputChatBox("No player found.", player, 255, 0,0) end else outputChatBox("#FF0000Correct command is : #FEFE22/give [(part of) nick] [amount]", player, 0, 255, 0, true) end else outputChatBox("You need to register and login in order to share coins!", player, 255,153,0) end end addCommandHandler("give", giveCash) 1 Link to comment
koragg Posted October 24, 2016 Author Share Posted October 24, 2016 @Walid wow, didn't expect someone would just write me the fixed script Thanks, it works perfect + is less lines now. Link to comment
Walid Posted October 24, 2016 Share Posted October 24, 2016 29 minutes ago, koragg said: @Walid wow, didn't expect someone would just write me the fixed script Thanks, it works perfect + is less lines now. You are welcome 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