Controllable Fairy And Darkrp

Yes i was going to add the controllable fairy to my darkrp server as an entity and it works fine but my problem is that the fairy stays in one place and does not follow the player once it is spawned from the darkrp menu. in the init.lua i need to somehow make it where it makes the owner of the fairy once it is spawned from the darkrp menu. Since my lua knowledge is not as good as i would like it to be i need some help on this issue.

–[[----------------------------------------------------------------------]]–
–[[ Fairy made by Killer HAHA (Robbis_1) ]]–
–[[ Edited py Knarf ]]–
–[[ Idea by CapsAdmin and originally made for his server ]]–
–[[----------------------------------------------------------------------]]–

– SETTINGS –
local Settings = {}
– When a player spawns, should a fairy spawn with them?
– Warning: This will spawn the fairy with the DEFAULT name and color!
Settings.SpawnWithPlayer = true

-- Shoud it only spawn with admins?
Settings.SpawnWithPlayerAdminOnly = false

-- The name it should have when it spawns, if the player doesn't have any own name set
Settings.SpawnName = "Fairy C"
	-- The color it should have when it spawns, if the player doesn't have any own color set
Settings.SpawnColor = Color(150, 255, 255)

– SETTINGS –

AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“shared.lua”)

include(‘shared.lua’)

local Fairies = {}

local EntModel = Model(“models/props_junk/popcan01a.mdl”)
function ENT:Initialize()
self:SetModel(EntModel)
self:PhysicsInitSphere(4)
self:SetCollisionBounds(Vector()*-2, Vector()*2)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
self:DrawShadow(false)
self.PauseTimer = CurTime()
self.HealDelay = CurTime()
self.Entity.LastOnPickedUp = CurTime()

local PhysObj = self:GetPhysicsObject()
	
if (PhysObj:IsValid()) then
	PhysObj:Wake()
	PhysObj:EnableGravity(false)
end

end

function ENT:MakeTrail(Color)
if (self.Entity.Trail) and (self.Entity.Trail:IsValid()) then
self.Entity.Trail:Remove()
end

self.Entity.Trail = util.SpriteTrail(self.Entity, 0, Color, false, 10, 0, 1, 0.125, "trails/laser.vmt")

end
– This is the main spawning function
local function SpawnFairy(Ply, Pos, NoPrint)
local PlyIndex = Ply:EntIndex()
if (Fairies[PlyIndex]) then
Fairies[PlyIndex].Num = Fairies[PlyIndex].Num + 1
else
Fairies[PlyIndex] = {}
Fairies[PlyIndex].Num = 1
Fairies[PlyIndex].Fairies = {}
end

-- Name
local Fairyname = Ply:GetInfo("fairy_name")  
	if (Fairyname == "") then
	Fairyname = Settings.SpawnName
end

	if (Fairies[PlyIndex].Num > 1) then
	Fairyname = Fairyname..Fairies[PlyIndex].Num-1
end

-- Color
local ColorR, ColorG, ColorB = Ply:GetInfo("fairy_color_r"), Ply:GetInfo("fairy_color_g"), Ply:GetInfo("fairy_color_b")
	if (ColorR == "") or (ColorG == "") or (ColorB == "") then
	ColorR = Settings.SpawnColor.r
	ColorG = Settings.SpawnColor.g
	ColorB = Settings.SpawnColor.b
end

-- Spawn
local Ent = ents.Create("gmod_fairy")
Ent:SetPos(Pos)
Ent:SetColor(ColorR, ColorG, ColorB, 255)
Ent:SetBrightness(1)
Ent:SetLightSize(512)
Ent:SetNetworkedString("OwnerName", Ply:Name())
Ent:SetNetworkedString("Name", Fairyname)
Ent:Spawn()
Ent:Activate()

Ent.Num = Fairies[PlyIndex].Num
Ent.Player = Ply

Ent:MakeTrail(Color(ColorR, ColorG, ColorB, 255))

local Index = Ent:EntIndex()
Fairies[Index] = {}
Fairies[Index].Name = Fairyname
Fairies[Index].Owner = Ply

table.insert(Fairies[PlyIndex].Fairies, Index)

if not (NoPrint) then
	Ply:ChatPrint("Fairy spawned with name: " .. Fairyname)
end

return Ent

end

function ENT:SpawnFunction(Ply, Trace)
if not (Trace.Hit) then return end
local Fairy = SpawnFairy(Ply, Trace.HitPos + Trace.HitNormal * 4)
return Fairy
end

local function GetPlayerByName(Ply, TargetName)
if (TargetName) and (TargetName != “”) then
for k,v in pairs(player.GetAll()) do
if (string.find(string.lower(v:Name()), TargetName)) then
return v
end
end
end
end

local function GetFairies(Ply)
local Index = Ply:EntIndex()
local t = {}
local num = 0

if (Fairies[Index]) then
	for k,v in pairs(Fairies[Index].Fairies) do
		local Ent = ents.GetByIndex(v)
		
		if (Ent) and (Ent:IsValid()) then
			local Data = Fairies[v]
			
			if (Data) and (Data.Owner == Ply) then
				t[v] = Data
				num = num + 1
			end
		else
			Fairies[Index].Fairies[v] = nil
		end
	end
end

return t, num

end

function ENT:Reset()
self.Entity:SetNetworkedString(“Behavior”, “”)
self.Entity:SetOwner(NULL)
self.Entity.HealTarget = nil
self.Entity.Following = nil
self.Entity.Attacking = nil
self.Entity.Stay = nil
self.Entity.Bring = nil
self.Entity.IsLeaving = nil
self.Entity.TellTarget = nil

if (self.Entity:GetLightSize() != 256) then
	self.Entity:SetLightSize(256)
end
if (self.Entity:GetBrightness() != 1) then
	self.Entity:SetBrightness(1)
end

end

function ENT:Pause(Time)
self.Entity.PauseTimer = CurTime() + Time
self.Entity:SetNetworkedFloat(“Pause”, CurTime() + Time)
end

function ENT:Think()
if (self.Entity.Player) and not (self.Entity.Player:IsConnected()) then
self.Entity:Remove()
end

if ( self.Occupied && !ValidEntity( self.Occupant ) ) then
	self.Occupied = false;
	return;
end

if ( self.Occupied && self.Occupant:KeyDown( IN_USE ) && ( !self.NextUse || self.NextUse <= CurTime( ) ) ) then
	self.NextUse = CurTime() + 0.5;
	self:Leave();
end


if (self.Occupied || ValidEntity( self.Occupant ) ) then
	self.Entity:NextThink(CurTime())
	return true
end

if (self.Entity.Player) and (self.Entity.Player:IsValid()) and (self.Entity.Player:Alive()) then
	if (self.Entity.PauseTimer < CurTime()) then
		local Ply = self.Entity.Following or self.Entity.Player
		
		local Behavior = self.Entity:GetNetworkedString("Behavior")
		local Pos 
		if (Behavior == "Heal") and (self.Entity.HealTarget:Alive()) then
			Pos = self.Entity.HealTarget:GetPos()
			Pos.z = Pos.z + 40
		elseif (Behavior == "Light") then
			local Trace = util.QuickTrace(Ply:EyePos(), Ply:GetAimVector() * 200, {Ply, self.Entity})
			
			Pos = Trace.HitPos
		elseif (Behavior == "Attacking") then
			Pos = self.Entity.Attacking:GetPos()
			Pos.z = Pos.z + 40
			
			if not (self.Entity.Attacking:Alive()) then
				self:Reset()
			end
		elseif (Behavior == "Stay") then
			Pos = self.Entity.Stay
		elseif (Behavior == "Tell") then
			Pos = self.Entity.Following:GetPos()
			Pos.z = Pos.z + 62
			
		else
			Pos = Ply:GetShootPos()
			local Trace = util.QuickTrace(Pos, Vector(0, 0, 70), {Ply, self.Entity})
			
			Pos.z = Pos.z + Trace.Fraction * 70 - 30
			
			local AimVec = Ply:GetAimVector()
			AimVec.z = AimVec.z/-3
			Pos = Pos + AimVec * -80
			
			if (self.Entity.IsLeaving) then
				Pos = self.Entity.Stay
				
				Pos.x = Pos.x + math.sin(CurTime()) * math.random(-50, 50)
				Pos.y = Pos.y + math.cos(CurTime()) * math.random(-50, 50)
				Pos.z = Pos.z + math.cos(CurTime()) * math.random(-50, 50)
			elseif (self.Entity.IsLaughing) then
				Pos.x = Pos.x + math.sin(CurTime()) * math.random(-250, 250)
				Pos.y = Pos.y + math.cos(CurTime()) * math.random(-250, 250)
				Pos.z = Pos.z + math.cos(CurTime()) * math.random(-250, 250)
			else
				local PlyIndex = self.Entity.Player:EntIndex()
				local Offset = math.rad((self.Num/Fairies[PlyIndex].Num) * 360)
				Pos.x = Pos.x + math.cos(CurTime() + Offset) * math.min(75, 15 + Fairies[PlyIndex].Num * 15)
				Pos.y = Pos.y + math.sin(CurTime() + Offset) * math.min(75, 15 + Fairies[PlyIndex].Num * 15)
				
				Pos.z = Pos.z + math.sin(CurTime()) * 10
			end
		end
		
		local Angle = Pos - self.Entity:GetPos()
		local Distance = Angle:Length()
		local PhysObj = self.Entity:GetPhysicsObject()
		local Velocity = PhysObj:GetVelocity()
		
		if (Behavior == "Heal") and (self.Entity.HealTarget:Alive()) then
			local HP = self.Entity.HealTarget:Health()
			
			if (HP < 100) then
				if (Distance < 20) and (self.Entity.HealDelay < CurTime()) then
					self.Entity.HealTarget:SetHealth(HP + 1)
					
					if (self.Entity:GetLightSize() != 512) then
						self.Entity:SetLightSize(512)
					end
					
					self.Entity.HealDelay = CurTime() + 0.1
				else
					if (self.Entity:GetLightSize() != 256) then
						self.Entity:SetLightSize(256)
					end
				end
			else
				self:Talk(Talk.Heal.Done)
				
				self:Reset()
				
				if (self.Entity:GetLightSize() != 256) then
					self.Entity:SetLightSize(256)
				end
			end
			
			Distance = Distance + 50
		elseif (Behavior == "Light") then
			if (self.Entity.Player:KeyDown(IN_FORWARD)) or (self.Entity.Player:KeyDown(IN_BACKWARD)) then
				Pos.x = Pos.x + math.cos(CurTime()) * math.random(-30, 30)
				Pos.y = Pos.y + math.sin(CurTime()) * math.random(-30, 30)
				
				Angle = (Pos - self.Entity:GetPos())
				
				PhysObj:ApplyForceCenter(Angle * 1.15)
			end
		elseif (Behavior == "Attacking") then
			if (Distance < 30) and (self.Entity.Attacking) then
				local HP = self.Entity.Attacking:Health()
				self.Entity.Attacking:SetHealth(HP - math.random(15, 20))
				
				local Effect = EffectData()
				Effect:SetOrigin(self.Entity:GetPos())
				util.Effect("fairy_explosion", Effect)
				
				if (self.Entity.Attacking:Health() > 0) and (self.Entity.Attacking:Alive()) then
					self:Pause(1)
				else
					self:Talk(string.format(Talk.Attack.Kill, self.Entity.Attacking:Name()))
					self.Entity.Attacking:Kill()
					
					self:Pause(2)
					self:Reset()
				end
			end
			
			if (Distance < 500) then
				local D = math.abs(Distance - 500)
				PhysObj:ApplyForceCenter(Angle * D/50 + Velocity/3 * -1)
			end
		end
		
		PhysObj:ApplyForceCenter(Angle * Distance/500 + Velocity/3 * -1)
	else
		local PhysObj = self.Entity:GetPhysicsObject()
		local Velocity = PhysObj:GetVelocity()
		Velocity.z = Velocity.z + 300
		
		PhysObj:ApplyForceCenter(Velocity/15 * -1)
		
		-- Update the pos so it doesn't go back
		if (self.Entity.IsLeaving) then
			self.Entity.Stay = self.Entity:GetPos()
		end
	end
elseif not (self.Entity.PlayerDead) then
	self.Entity.PlayerDead = true
else
	local PhysObj = self.Entity:GetPhysicsObject()
	local Velocity = PhysObj:GetVelocity()
	PhysObj:ApplyForceCenter(Velocity/15 * -1)
end

if (self.Entity.PlayerDead) and (self.Entity.Player:Alive()) then
	self.Entity:SetPos(self.Entity.Player:GetShootPos())
	self.Entity:GetPhysicsObject():EnableMotion(true)
	self.Entity.PlayerDead = nil
	self:Reset()
end

self.Entity:NextThink(CurTime())
return true

end

function ENT:OnTakeDamage(DmgInfo)
self.Entity:TakePhysicsDamage(DmgInfo)
end

function ENT:PhysicsCollide(Data, PhysObj)
if (Data.Speed > 500) and (Data.DeltaTime > 0.2 ) then
if (self.Entity.Attacking) then
self:Talk(Talk.Attack.Fail)
self:Reset()
elseif (self.Entity.TellMessage) then
self:Talk(Talk.Tell.Fail, 0, true)
self:Reset()
end

	self:Pause(1)
end

-- We don't want it to try get to a position outside the world
if (self.Entity.IsLeaving) then
	self.Entity.Stay = Data.HitPos
end

local LastSpeed = Data.OurOldVelocity:Length()
local NewVelocity = PhysObj:GetVelocity()
NewVelocity:Normalize()

LastSpeed = math.max(NewVelocity:Length(), LastSpeed)

local TargetVelocity = NewVelocity * LastSpeed * 0.8

PhysObj:SetVelocity(TargetVelocity)

end

function ENT:OnRemove()
local Index = self.Entity:EntIndex()
local PlyIndex = self.Entity.Player:EntIndex()
FairyNumber = 0
Fairies[Index] = nil

if (self.Entity.Player:IsConnected()) then
	-- Player is still on the server
	Fairies[PlyIndex].Num = Fairies[PlyIndex].Num - 1
	
	local Num = 0
	for k,v in pairs(Fairies[PlyIndex].Fairies) do
		local Ent = ents.GetByIndex(v)
		
		if (Ent) and (Ent:IsValid()) then
			Num = Num + 1
			Ent.Num = Num
		end
	end
else
	-- The player left or something, doesn't exist anyway
	Fairies[PlyIndex] = nil
end

end

function ENT:ChangeColor(Color)
self.Entity:SetColor(Color.r, Color.g, Color.b, 255)
self:MakeTrail(Color)

self.Entity:Reset()

end

local function PlayerInitialSpawn(Ply)
– Mod by Baaleos
if (Settings.SpawnWithPlayer and gmod.GetGamemode().Name == “Sandbox”) or ((Ply:IsAdmin()) and Settings.SpawnWithPlayerAdminOnly) then
SpawnFairy(Ply, Ply:GetShootPos())
end
end

///////////////////////////////////////////
// Occupation and leaving the Fairy //
//////////////////////////////////////////

function ENT:Use( Player )

if ( !self.Occupied ) then

	self.NextUse = CurTime() + 1;
	self:Occupy( Player );
	
end

end

function ENT:Occupy( Player )

if ( !ValidEntity( Player ) ) then return end

Player:StripWeapons();
Player:Spectate( OBS_MODE_CHASE );
Player:SpectateEntity( self );
Player:SetMoveType( MOVETYPE_OBSERVER );
Player:CrosshairDisable();

self:SetPhysicsAttacker( Player );
self:PhysWake();

self.Occupied = true;
self.Occupant = Player;

end

function ENT:Leave( )

if ( !ValidEntity( self.Occupant ) ) then return; end

self.Occupant:UnSpectate();
self.Occupant:SetMoveType( MOVETYPE_WALK );
self.Occupant:KillSilent();
self.Occupant:Spawn();
self.Occupant:SetPos( self:GetPos() + Vector( 0, 0, 36 ) );
self.Occupant:CrosshairEnable();

self.Occupied = false;
self.Occupant = nil;

end

hook.Add(“PlayerInitialSpawn”, “SpawnFairy”, PlayerInitialSpawn)

///////////////////
// Physics Stuff //
///////////////////

function ENT:PhysicsUpdate( PhysObj )

if ( !self.Occupied || !ValidEntity( self.Occupant ) ) then return end

local Force = Vector( 0, 0, 0 );
local Angles = self.Occupant:EyeAngles();
local ForceMulty = 1;

if ( self.Occupant:KeyDown( IN_SPEED ) ) then
	ForceMulty = 5;
end

if ( self.Occupant:KeyDown( IN_FORWARD ) ) then
	Force = Angles:Forward() * ForceMulty;
end

if ( self.Occupant:KeyDown( IN_BACK ) ) then
	Force = Force - Angles:Forward() * ForceMulty;
end

if ( self.Occupant:KeyDown( IN_MOVELEFT ) ) then
	Force = Force - Angles:Right() * ForceMulty;
end

if ( self.Occupant:KeyDown( IN_MOVERIGHT ) ) then
	Force = Force + Angles:Right() * ForceMulty;
end

if ( self.Occupant:KeyDown( IN_JUMP ) ) then
	PhysObj:SetVelocity (Vector( 0, 0, 0 ));
else
	PhysObj:ApplyForceOffset( Force, self:GetPos() );
end

end

Use [NOPARSE]


your code

[/NOPARSE] around that shit.