• Donation Box - DarkRP
    13 replies, posted
Hello! I was bored so I decided to make something worth my time, or is it? :O I've simply made a donation box for hobos on darkrp however when someone puts x amount into the box ( by money touching the box ) it simply doubles the amount e.g I put iin $200 it'll say $400. Any help please? INIT.LUA [CODE] include("shared.lua") AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") function ENT:Initialize() self:SetModel( "models/props_c17/cashregister01a.mdl" ) self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType( MOVETYPE_VPHYSICS ) self:SetSolid( SOLID_VPHYSICS ) self:SetUseType( SIMPLE_USE ) self:SetNWInt("Money",0); local phys = self:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end end function ENT:Touch(ent) if(ent:GetClass() == "spawned_money")then self:SetNWInt("Money",self:GetNWInt("Money") + ent:Getamount()) ent:Remove() end end function ENT:Use(u) if(self:GetNWInt("Money")>0)then u:addMoney(self:GetNWInt("Money")) u:ChatPrint("You take $"..self:GetNWInt("Money")) self:SetNWInt("Money",0) else u:ChatPrint("There ain't shit in here!") u:ChatPrint("There ain't shit in here!") end end [/CODE]
What exactly is the problem you are having? Also, You need to restrict the people who can use the entity. You also need to redo your Touch function, as it will get called multiple times for one entity. (eg, if you put in $20, $160 might get added)
[QUOTE=James xX;47585878]What exactly is the problem you are having? Also, You need to restrict the people who can use the entity. You also need to redo your Touch function, as it will get called multiple times for one entity. (eg, if you put in $20, $160 might get added)[/QUOTE] Uh, I don't understand?
[QUOTE=Chickengbs;47585891]Uh, I don't understand?[/QUOTE] A- What specifically are you asking in this thread. Are you getting errors, or asking how to do something? B- Anyone can press enter on your entity and get the money at the moment. Change it so only the owner can. C- You assume that the touch entity will only get called once when money is dropped on it, whereas it might get called multiple times. You need to maybe store a table of money that has touched the entity, and check, when money touches it, if it's in the table. If it is, don't do anything, if not, do your code and add it to the table.
I suppose making it so only the owner can take the money I'll work out that myself but the issue is when I drop $200 it basiclly doubles when it's touched the enitity so when I press E to get my $200 it'll give me $400 instead.
[QUOTE=Chickengbs;47585979]I suppose making it so only the owner can take the money I'll work out that myself but the issue is when I drop $200 it basiclly doubles when it's touched the enitity so when I press E to get my $200 it'll give me $400 instead.[/QUOTE] Oh I've understood. You didn't intend to double the amount. I thought it was a feature not a bug. You can solve it by doing part C of my previous post.
[QUOTE=James xX;47586006]Oh I've understood. You didn't intend to double the amount. I thought it was a feature not a bug. You can solve it by doing part C of my previous post.[/QUOTE] Uh, could I get an assistance please, my hair is falling out over this -.-
[QUOTE=Chickengbs;47586036]Uh, could I get an assistance please, my hair is falling out over this -.-[/QUOTE] Something like this: [lua] include("shared.lua") AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") function ENT:Initialize() self:SetModel( "models/props_c17/cashregister01a.mdl" ) self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType( MOVETYPE_VPHYSICS ) self:SetSolid( SOLID_VPHYSICS ) self:SetUseType( SIMPLE_USE ) self:SetNWInt("Money",0); self.EntityLibrary = {} local phys = self:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end end function ENT:Touch(ent) if (self.EntityLibrary[ent]) then return end if(ent:GetClass() == "spawned_money")then self:SetNWInt("Money",self:GetNWInt("Money") + ent:Getamount()) self.EntityLibrary[ent] = true ent:Remove() end end function ENT:Use(u) if(self:GetNWInt("Money")>0)then u:addMoney(self:GetNWInt("Money")) u:ChatPrint("You take $"..self:GetNWInt("Money")) self:SetNWInt("Money",0) else u:ChatPrint("There ain't shit in here!") end end [/lua] I haven't tested this but it should work. If not, it should show you how to do it anyway. Also, although it makes no difference to your code what so ever, consider using tabs instead of spaces to do indentation. It keeps your code a bit cleaner, and makes it easier to see chunks.
Thank you, that worked. Can you tell me what the difference you made, I don't get it..
In the Initialize function, I created a table called EntityLibrary. In the Touch function, I stopped any code from running (by returning) if the entity was in the EntityLibrary. If it wasn't, the code runs, and adds the entity to EntityLibrary so the function doesn't fun multiple times for a single entity. If you don't understand, please don't hesitate to ask.
Add my steam please ? xD
I would need to know your steam first.
STEAM_0:1:49281921 here we go ;3
[QUOTE=Chickengbs;47586334]STEAM_0:1:49281921 here we go ;3[/QUOTE] Your steam name. I added you anyway, but in future, when you ask someone to add you, don't give them your steamID, as it means they have to go look up your community name.
Sorry, you need to Log In to post a reply to this thread.