Adding score when touching a certain object

Is there any way to make it so when you touch a certain object, like a map entity or a prop or something, that it adds +1 to your score? I’m making my gamemode so that whoever has the highest score at the end wins, and I need to know this, thanks.

^that in the entity’s StartTouch?

(I think you can do AddScore w/ player ents too, if not you might need to do AddFrags or write your own scoring system for non team-based)

Ahh, alright. How would I go about disabling the AddFrags after one person touches it?

Going to use AddFrags instead because it’s a player function so I can make it add the score to only that one person.

there’s a million ways to do that, but an easy one is to have a boolean value in the ent determining whether it’s active for scoring. when it initializes at round start, active=true … when someone touches it, if active==true it adds one to the score and sets active to false.

Okay, how would I choose where to place the SENT?

Also, sorry for all the questions, I’m just finding this a bit difficult.

How I’ve been creating ents:

first this

then this

then this

If they’re statically positioned, ents can just be placed in hammer too, obviously

For placing all of your entities in pre-chosen positions trough lua there are several methods available. One of the simplest ways when you’re mapping yourself is to place point entities that will act as spawn points for your entities, you just need to give them a special class name such as info_myent.

Also if they’re only meant to give points once, maybe you could simply [remove]( them once they have been picked up?

Alright, going to try your suggestions, thanks. Will post results later.

Is this good for my purpose?

ents.Create( "end_point" )

function ENT:StartTouch( ent )
	if ( ent:IsValid() and ent:IsPlayer() ) then
		for k,v in pairs(player.GetAll()) do
			player.GetAll(1):AddFrags(1) then
			Entity:Remove( "end_point" )

I keep getting two errors mostly. 1.

Attempted to create unknown entity type end_point!
Can't init end_point

It’s not finding the entity, what kind of entity should the SENT be?

grapplingrace\gamemode\shared.lua:75: unexpected symbol near 'then'

Line 75 is “player.GetAll(1):AddFrags(1) then”

It keeps reverting it back to base, so I can’t test anything.

Problem 2 : remove then.

Problem 1 : You need to define the entity before you can create it ingame. So you would define it in it’s own lua file in lua/entities/entities/end_point/
(init.lua for serverside hooks, cl_init.lua for clientside hooks and shared.lua for shared ones)

and then create it in your gamemode that way:

local ent = ents.Create("end_point")
ent:SetPos(Vector(0,0,0)) -- Replace the vector by the position it should spawn at
ent:Spawn() -- This is what actually makes it spawn into the world.

You should probably [visit the wiki]( and look for tutorials and examples on scripted entities. You should also take a look at other people’s scripted entities to see how it’s done.


Also Entity:Remove( “end_point” ) should be self:Remove()

Alright, I’m trying to get the entity to be spawnable via a map entity, not via co-ordinates because then I’d need a different co-ordinate for each map, wouldn’t I? I defined it in it’s own folder, yet it keeps giving me the “Attempted to create unknown entity type end_point!
Can’t init end_point” error.

That means the entity’s code has errors in it and is not registered. Look at all of your console’s log, especially at the top, and look for lua errors in that folder.

okay, got these errors.

entities/end_piont/init.lua:3: attempt to index global 'ent' (a nil value)
Folder	=	entities/end_piont
Couldn't register Scripted Entity end_piont - the Type field is empty!

Attempted to create unknown entity type end_piont!
Can't init end_piont

also, would the code in that folder go like so?


		function ent:StartTouch( end_piont )
	if ( ent:IsValid() and ent:IsPlayer() ) then
		for k,v in pairs(player.GetAll()) do

Download this, it isn’t made by me but it has the functions that seem to do exactly what you want. If you look in… my guess is the init.lua file for the terms “GravGun” then I’m sure you’ll find what you’re looking for!

Edit: Though I suppose you’d do it without the gravgun bit, and use ent.touch instead.