Error: attempt to Call a nil value

Hello there :slight_smile:

I try to sum 2 Money-Bags (Dark-RP) with a function, that searches every Money-Bag, and then checks, if theres another moneybag near the first one. (To reduce laggs) :v:

This is my Code:



 -- GELD IMMER IN BÜNDEL ZUSAMMENPACKEN
timer.Create("searchmoney", 3, 0, SearchMoney)
function SearchMoney()
    for i, entity in ipairs( ents.FindByModel("models/props/cs_assault/money.mdl") ) do
		if entity:IsMoneyBag() then
			CollectMoney(entity, 2)
		end
    end
     
    return "Money Found";
     
end
function CollectMoney( findentity, range )
     
    local nearestEnt;
     
    for i, entity in ipairs( ents.FindByModel("models/props/cs_assault/money.mdl") ) do
		if entity:IsMoneyBag() then
				local distance = findentity:GetPos():Distance( entity:GetPos() );
				if( distance <= range ) then
					local comm = findentity.amount + entity.amount
					DarkRPCreateMoneyBag(findentity:Pos(), comm)
				end
		end
    end
     
    return "Money Summarized";
     
end
--------------------------------------------


But if i run the Gamemode i get the following Error:

What could be the Problem?

Best regards,
Northdegree

Put timer.Create(“searchmoney”, 3, 0, SearchMoney)

UNDER the other functions.
When you set the timer, the SearchMoney function doesnt exist yet.

Thanks mate, this worked so far… :slight_smile:

Now another Problem:
The first Money-Bag and the second Money-Bag are getting removed… then they create a new one …

But this happens to BOTH (first and second) moneybags…

My Opinion:
The “for-loop” runs too fast, so that both Moneybags are working seperately … :S

Current Code:



 -- GELD IMMER IN BÜNDEL ZUSAMMENPACKEN
function SearchMoney()
	local range = 7
    for i, findentity in ipairs( ents.FindByModel("models/props/cs_assault/money.mdl") ) do
		if findentity:IsMoneyBag() then
			    local nearestEnt;
				for i, entity in ipairs( ents.FindByModel("models/props/cs_assault/money.mdl") ) do
					if entity:IsMoneyBag() then
							local distance = findentity:GetPos():Distance( entity:GetPos() );
							if (distance <= range) and entity != findentity then
								local comm = tonumber(findentity.dt.amount + entity.dt.amount) or 0
								local oldpos = findentity:GetPos()
								findentity:SetPos(findentity:GetPos() + Vector(10, 10, 10))
								entity:SetPos(entity:GetPos() + Vector(10, 10, 10))
								findentity:Remove()
								entity:Remove()
								print("Moneybag 1: "..findentity.dt.amount.."$, Moneybag 2: "..entity.dt.amount.."
")
								DarkRPCreateMoneyBag(oldpos + Vector(0,0,5), comm)
								print("Created new Moneybag! (Amount: "..comm..")")
							end
					end
				end
		end
    end
     
    return "Money Found";
     
end

timer.Create("searchmoney", 2, 0, SearchMoney)
--------------------------------------------


Can you put it in [lua] tags?

Nevermind, fixed :slight_smile:

Atheos (DarkRP-Scripter) helped me :slight_smile:

DarkRP-Scripter? Weird title…