DreaM40BG Posted April 17, 2020 Share Posted April 17, 2020 Hello, I was trying for 30 minutes to fix that problem and I couldn't. I tried to see the MTA wiki, still I didn't get it. The problem is: For example ,there is no player "test" online in the server and I write /give test 2 and it takes 2$ from me and gives to nobody. How to fix that? 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("give", function(player, cmd, name, amount) local amount = tonumber(amount) if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix local target = findPlayer(name) local money = getPlayerMoney(player) local targetid = getPlayerFromName(target) or outputChatBox("Player not found", player) 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.", player, 255, 0, 0, false) end end end ) I tried with "playerid" and others and still not working. Link to comment
jakson Posted April 17, 2020 Share Posted April 17, 2020 1 hour ago, DreaM40BG said: local targetid = getPlayerFromName(target) or outputChatBox("Player not found", player) Replace this to this local targetid = getPlayerFromName(target) if targetid then 1 Link to comment
DreaM40BG Posted April 17, 2020 Author Share Posted April 17, 2020 1 hour ago, jakson said: Replace this to this local targetid = getPlayerFromName(target) if targetid then Don't work. Link to comment
jakson Posted April 17, 2020 Share Posted April 17, 2020 14 minutes ago, DreaM40BG said: Don't work. Any error Shown in debug? 2 hours ago, DreaM40BG said: end end end ) I think you forgot to add one more end ? You should add 4 ends not 3 Link to comment
DreaM40BG Posted April 17, 2020 Author Share Posted April 17, 2020 Yep, I did. Still not working, it doesn't send to anybody. 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("give", function(player, cmd, name, amount) local amount = tonumber(amount) if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix local target = findPlayer(name) local money = getPlayerMoney(player) local targetid = getPlayerFromName(target) if targetid then 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.", player, 255, 0, 0, false) end end end end ) Link to comment
Spakye Posted April 18, 2020 Share Posted April 18, 2020 what about : 26 if targetid ~= false then Link to comment
DreaM40BG Posted April 18, 2020 Author Share Posted April 18, 2020 [20-04-18 17:10:26] WARNING: givemoney/server.Lua:25: Bad argument @ 'getPlayerFromName' [Expected string at argument 1, got player] Not working. :< 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("give", function(player, cmd, name, amount) local amount = tonumber(amount) if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix local target = findPlayer(name) local money = getPlayerMoney(player) local targetid = getPlayerFromName(target) if targetid ~= false then 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.", player, 255, 0, 0, false) end end end end ) Link to comment
-Ilker. Posted April 18, 2020 Share Posted April 18, 2020 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("give", function(player, cmd, name, amount) local amount = tonumber(amount) if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix local targetid = findPlayer(name) local money = getPlayerMoney(player) if targetid ~= false then 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.", player, 255, 0, 0, false) end end end end ) Link to comment
Prioq Posted April 18, 2020 Share Posted April 18, 2020 (edited) 17 minutes ago, DreaM40BG said: [20-04-18 17:10:26] WARNING: givemoney/server.Lua:25: Bad argument @ 'getPlayerFromName' [Expected string at argument 1, got player] Not working. :< 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("give", function(player, cmd, name, amount) local amount = tonumber(amount) if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix local target = findPlayer(name) local money = getPlayerMoney(player) local targetid = getPlayerFromName(target) if targetid ~= false then 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.", player, 255, 0, 0, false) end end end end ) target is the player you're looking for, no need to get him from name local targetid = target Edited April 18, 2020 by Prioq Link to comment
DreaM40BG Posted April 19, 2020 Author Share Posted April 19, 2020 (edited) Okay it takes money but don't give to the 2nd player. Not working, also no messages. 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("give", function(player, cmd, name, amount) local amount = tonumber(amount) if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix local targetid = findPlayer(name) local money = getPlayerMoney(player) if targetid ~= false then 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.", player, 255, 0, 0, false) end end end end ) Edited April 19, 2020 by DreaM40BG Link to comment
Spakye Posted April 19, 2020 Share Posted April 19, 2020 i dont understand why you need the findPlayer function you could do like that addCommandHandler("give", function(player, cmd, name, amount) local amount = tonumber(amount) if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix local target = getPlayerFromName (name) local money = getPlayerMoney(player) if target ~= false then 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.", player, 255, 0, 0, false) end end end Link to comment
DreaM40BG Posted April 19, 2020 Author Share Posted April 19, 2020 13 minutes ago, Spakye said: i dont understand why you need the findPlayer function you could do like that addCommandHandler("give", function(player, cmd, name, amount) local amount = tonumber(amount) if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix local target = getPlayerFromName (name) local money = getPlayerMoney(player) if target ~= false then 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.", player, 255, 0, 0, false) end end end It is not working again ;/ Link to comment
Spakye Posted April 19, 2020 Share Posted April 19, 2020 (edited) what error does it give ? edit : i notice i forgot ) at then end of the code, if you copy pasted my code then add ) at the end Edited April 19, 2020 by Spakye Link to comment
Moderators Patrick Posted April 19, 2020 Moderators Share Posted April 19, 2020 I told you before, learn Lua basics please before you ask in this section! Some mistake what I suddenly found 1 Link to comment
DreaM40BG Posted April 19, 2020 Author Share Posted April 19, 2020 I didn't expect that it wouldn't be closed, but you are right. Thanks, lock the topic it is working now. 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