Hello, I have been making an entity that allows you to purchase either Health or Armour. Everything works fine apart from: the code gets executed on the first player that first joins and not the player using the entity. I really need the armour and health to get added on to the player that uses the entity.
Any help is appreciatted!
[B]init.lua[/B]
[code]AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
util.AddNetworkString("health")
util.AddNetworkString("panel")
util.AddNetworkString("armour")
function ENT:Initialize()
self:SetModel("models/props_interiors/VendingMachineSoda01a.mdl")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
self:SetUseType( SIMPLE_USE )
local phys = self:GetPhysicsObject()
if phys:IsValid() then
phys:Wake()
end
end
function ENT:Use(activator, caller)
if IsValid(caller) and caller:IsPlayer() then
net.Start("panel")
net.Send(caller)
end
end
net.Receive("health",function()
if
Entity( 1 ):getDarkRPVar("money") > 500 and Entity( 1 ):Health() < 100 then
Entity( 1 ):addMoney(-500)
Entity( 1 ):SetHealth( Entity( 1 ):GetMaxHealth() )
else
Entity( 1 ):PrintMessage(HUD_PRINTTALK ,"You cannot purchase Health right now!")
end
end)
net.Receive("armour",function()
if
Entity( 1 ):getDarkRPVar("money") > 1000 and Entity( 1 ):Armor() < 100 then
Entity( 1 ):addMoney(-1000)
Entity( 1 ):SetArmor( Entity( 1 ):GetMaxHealth() )
else
Entity( 1 ):PrintMessage(HUD_PRINTTALK ,"You cannot purchase Armour right now!")
end
end)[/code]
[B]cl_init.lua[/B]
[code]include("shared.lua")
surface.CreateFont( "Font", {
font = "Roboto", -- Use the font-name which is shown to you by your operating system Font Viewer, not the file name
extended = false,
size = 80,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = true,
additive = false,
outline = false,
} )
function ENT:Draw()
self:DrawModel()
local ang = self:GetAngles()
ang:RotateAroundAxis(self:GetAngles():Right(),270)
ang:RotateAroundAxis(self:GetAngles():Forward(),90)
cam.Start3D2D(self:GetPos()+Vector(21,0,10),ang,0.1)
draw.SimpleText("HP and Armour","Font",0,0,Color(255,50,50),1,1)
cam.End3D2D()
end
net.Receive("panel",function()
local frame = vgui.Create("DFrame")
frame:SetSize(600,200)
frame:Center()
frame:SetVisible(true)
frame:MakePopup()
frame:SetDraggable(false)
frame:SetTitle("Health and Armour")
frame:SetBackgroundBlur(true)
frame:ShowCloseButton(true)
frame.Paint = function(s,w,h)
draw.RoundedBox(5,0,0,w,h,Color(40,40,40))
end
local buttonhp = vgui.Create("DButton", frame)
buttonhp:SetPos(10,30)
buttonhp:SetSize(280,160)
buttonhp:SetText("Buy Health $500")
buttonhp.DoClick = function()
net.Start("health")
net.SendToServer()
frame:Close()
end
local buttonarm = vgui.Create("DButton", frame)
buttonarm:SetPos(310,30)
buttonarm:SetSize(280,160)
buttonarm:SetText("Buy Armour $1000")
buttonarm.DoClick = function()
net.Start("armour")
net.SendToServer()
frame:Close()
end
end)[/code]
[B]shared.lua[/B]
[code]ENT.Type = "anim"
ENT.Base = "base_gmodentity"
ENT.PrintName = "Health and Armour Vendor"
ENT.Spawnable = true[/code]
P.S I'm new to Lua
Entity( 1 ) will only target the first player.
Look at the function arguments for [URL="https://wiki.garrysmod.com/page/net/Receive"]net.Receive[/URL].
[B]ALSO - Very important:[/B]
DO NOT ever trust the client like you do here. From what you have created I could make a clientsided script that calls
[CODE]net.Start("health") net.SendToServer()[/CODE] to buy health everytime my health goes below 80% without me having to do anything, for an example.
Check so the player is close enough to the entity in the net.receive function.
Thank you for replying, I will attempt to fix it.
Sorry, you need to Log In to post a reply to this thread.