Hi, I don't know how to explain this well enough so I'll start with examples.
Let's say I'm creating an entity serverside with some player based hook - let's use player say. If I'll type "spawn" in chat it would spawn an entity.
if blah blah then
local example = ents.Create("entity_example")
if IsValid(example) then
example:SetPos(ply:GetShootPos())
example:SetOwner(ply) -- let's focus on this part
example:Spawn()
example:Activate()
end
Now for the init of the entity
//let's say most of the code is already written (im too lazy to create an ent for the purpose of an example)
local Owner /*by the way, is it wrong to define a variable without value(asking, cause I never see it in scripts)*/
function ENT:Initialize()
//let's also assume that code is already written
Owner = self:GetOwner()
end
function ENT:Touch(ply)
if(ply:IsPlayer() and ply != Owner then
if ply:Health() > 0 then
ply:TakeDamage( 10, Owner, Owner)
end
end
end
The purpose of the code (in case you don't know, but I think you do)is to damage everyone that touches the ent except the owner. The problem is that when another player spawns this entity, he gets the ownership. I already know why (i'm writing this to show you my line of thinking, if it helps)
Here's the fixed version:
function ENT:Touch(ply)
if(ply:IsPlayer() and ply != self:GetOwner() then
if ply:Health() > 0 then
ply:TakeDamage( 10, self:GetOwner(), self:GetOwner())
end
end
end
Here's where my question begins - Can I make a variable that's based on a player (without networking)? - A variable that has different values for each player (I somehow managed to explain this in 2 sentences (hopefully well enough), but it took some time to write all this earlier stuff so I'm not removing it )
I’m still somewhat confused by your question but I’ll answer as best as I can.
To begin,
local variable
Is completely valid code, initialized variable to nil, and is sometimes useful when reusing the same variable rather than re-localizing the variable for optimization reasons.
Now for your actual question, here’s an implementation of what I think you’re trying to achieve. Mind you this is written from my phone so ignore any formatting problems.
ENT.Base = “whatever”
—- more code
—- more code, you get the point
function ENT:Initialize()
self.NotHurtByThis = {}
—- other code
end
function ENT:AddEntityFriend(ply)
self.NotHurtByThis[ply] = true
end
function ENT:RemoveEntityFriend(ply)
self.NotHurtByThis[ply] = nil
end
function ENT:Touch(ply)
if ply:IsPlayer() and not self.NotHurtByThis[ply] then
—- Do damage here
end
end
—-
———— Elsewhere in another file
—-
local ent = ents.Create(“myent”)
ent:SetPos(whatever)
ent:AddEntityFriend(ply)
I didn't try it out yet, but just by looking at the code it seems logical to use tables >.< Thanks
You can define a variable without any value, it just gets initialized to nil. If you have it in your entity script like that then every single entity would use the new player when a new one spawns since its just a single variable not tied to anything. So if player1 spawns the entity first then player2 spawns the entity then both entities will use player2.
Also you should take a look at Entity/SetOwner as I don't think you want to use that. With objects like Players, Entities, Weapons, Tables, ect, you can just assign a value to it like this
ent.MyValue = 3
This is only set in the realm you set it though so in your example it would be set serverside. If a player tried to access that value, it wouldn't exist.
To get your script working like the way you want it to just set the player who created it as a variable on the entity just like
local example = ents.Create("entity_example")
if IsValid(example) then
example:SetPos(ply:GetShootPos())
example:Spawn()
example:Activate()
example.Owner = ply
end
And then just change the touch function to use self.Owner
Sorry, you need to Log In to post a reply to this thread.