Coding a Doctor NPC Gmod

Hello, I am about to begin coding a NPC for a garrysmod server. I wish to make a NPC with a doctor model (Already sorted) that you can interact with and it opens a derma that has press able buttons. When you press the button it sets the health for the user to 100 and takes money off them.

I am roughly sorted about the coding part but one problem I have is that the ENT:Use() function is serverside and the derma and chat.AddText are clientside. I have looked at tutorials on YouTube and they all say to make a folder called “YourNPCName” in the directory GarrysMod/garrysmod/lua/entities/“YourNPCName” then create 3 .lua files called “cl_init.lua, shared.lua and init.lua”
I have tried this and my NPC never works. I messed around for a while before finding out that is I create a .lua in the directory of GarrysMod/garrysmod/lua/entities/ then the NPC works. The only thing is that with the single file I don’t know how to differentiate between client, server and shared on a single file!

Thank you so much for your help! :slight_smile:

[lua]

– shared stuff

if SERVER then
– server stuff
else
– client stuff
end

– shared stuff

[/lua]

Thank you so much! For example could I do? :

(If you can not see the image above here it is) :
https://gyazo.com/3687f1afa273a3f769e862cabd19da69

No. You’re defining the function on the server and then checking whether it’s being called on the client.

So how would I do it? Preferable could you send me the code please :slight_smile:

Instead of picturing ur code, post it here in [.code] tags

Learn about networking and the proper use of SERVER and CLIENT

And just remove the if server statement.


function ENT:Use()
if CLIENT then
local Frame = vgui.Create( "DFrame" )
Frame:SetPos( 5, 5 )
Frame:SetSize( 300, 150 )
Frame:SetTitle( "" )
Frame:SetVisible( true )
Frame:SetDraggable( false )
Frame:ShowCloseButton( true )
Frame:MakePopup()
end
end

Would this be the right code then ?

You understand that if you check if it’s server, and then client, that the client check is redundant since you already checked that it’s the server?

Please use Net Library because it can help with Client and Server function problem.
You can call Client function on Server side just send a message to the Client from Server Side :smiley:

First of all, why are you trying to dev a server with no knowledge with this?

I am willing to learn about the Net Library. But if I was to create a NPC what directory and what documents would I create ?

–snip–

What? He can learn without developing for a server. He’d probably fuck things up with the server with no knowledge.

I was no where near saying “fucked up shit”, it was a question.

Explain to me how he is supposed to begin? Quit being such a jerk. I always see you around not doing anything to help but just saying some fucked up shit.

Here you go:

shared.lua





ENT.Type = "ai" -- Type of entity

ENT.Base = "base_ai" -- Entity base

ENT.PrintName     = "Medic NPC" -- Name of the entity

ENT.Author        = "Tupac Shakur"  -- Optional

ENT.Purpose       = "Heals player" -- Optional. Description of the entity

ENT.AutomaticFrameAdvance = true --Toggles automatic frame advancing for animated sequences on an entity. ( So the NPC can seem realistic )

ENT.Spawnable     = true -- Spawnable?

ENT.AdminSpawnable = false





init.lua





function ENT:Initialize()  -- What happens on the entity spawn

  self:SetModel( "models/Kleiner.mdl" ) -- Change to the model you want the NPC to be

  self:SetHullType( HULL_HUMAN ) 

  self:SetHullSizeNormal()

  self:SetNPCState( NPC_STATE_SCRIPT )

  self:SetSolid( SOLID_BBOX )

  self:CapabilitiesAdd( CAP_ANIMATEDFACE )

  self:SetUseType( SIMPLE_USE ) -- This is to prevent the menu opening so many times when e is hit.

  self:DropToFloor() 


end

util.AddNetworkString( "MedNPCMenuOpen" ) -- This is the string of the net message being sent around. 

function ENT:AcceptInput( name, activator, caller ) -- Accept the input, and store the information in name, activator, caller


  if name == "Use" and caller:IsPlayer() then -- if the player hit use then

    net.Start("MedNPCMenuOpen") -- Start the network string 

    net.Send(caller) -- Send this to the client that hit "use" on the npc

  end


end




cl_init.lua





net.Receive( "MedNPCMenuOpen", function()


    local Background = vgui.Create( "DFrame" )

    Background:SetSize( 500, 500 )

    Background:Center()

    Background:MakePopup()


end )




Useful things to look into

https://wiki.garrysmod.com/page/Derma_Basic_Guide This is a great derma guide, for your menu!

https://wiki.garrysmod.com/page/Net_Library_Usage This can be used to make it so that when a button is clicked, send a message to the server to set the players health.

Sorry if I suck at explaining, but I recommend you play around with the code and learn.

And mikey, when I say “fucked up shit” I’m referring to posts other than this.

Thank you so so much! This is just what I have been looking for! :smiley: YOU ARE AMAZING AT HELPING OTHERS!

Sure, give him the code done and don’t let him search… Soon he’ll be here again asking for more “examples”.
People don’t learn by getting what they want ready, they learn by doing it themselves or being taught HOW TO DO IT, not getting it done.

Sure, you helped him, you gave him what he wanted in the easiest way, but that is nowhere near teaching, its like if you gave a homework to a student with all the answers on it and expected them to don’t whine when they received a test with no answers on it already.

[editline]6th February 2016[/editline]

And just to fix the huge fuckup 2 people did with the ratings saying this was right, THIS IS WRONG CODE

ENTITY/Use is SERVERSIDE only so the menu would NEVER get created.