Hey everyone! At the moment I am writing a class for everything that lives in a game I am making. This class is set up as follows currently:
SelfMesh
(part in workspace)I have wrote a basic life function called Life:Animate() which at the moment allows the life form to perform five life choices and then they die.
While I am fairly confident the actual life cycle works - the issue seems to be in the possible life choice Life:GoToParent(), in which the life form is supposed to walk toward the person who spawned it. [tl;dr read this paragraph]
(currently the life forms seem to run toward the middle of the baseplate and start to pile up instead of running toward the person who spawned them)
This is the current setup, and my attempt [focus :Animate() and :GoToParent()]:
local Life = {} Life.__index = Life function Life.new(initType, trueObject, parent) local thisObject = {} setmetatable(thisObject, Life) thisObject.Type = initType thisObject.SelfMesh = trueObject thisObject.Guardian = parent thisObject.Occupied = false thisObject.Age = 0 return thisObject end function Life:GoToParent() self.Occupied = true local Parent = game:GetService("Players"):FindFirstChild(self.Guardian.Name).Character.HumanoidRootPart local SolvedCFrame = CFrame.new(self.SelfMesh.CFrame.p, Parent.Position) local Direction = (self.SelfMesh.Position).Unit game:GetService("TweenService"):Create(self.SelfMesh, TweenInfo.new(5), {Position = Vector3.new(self.SelfMesh.Position + Direction)}):Play() wait(5) self.Occupied = false end function Life:Animate(p) self.SelfMesh.CFrame = CFrame.new(p) * CFrame.new(0,1,0) repeat ----------- Life Cycle local LifeChoice = Random.new():NextInteger(1,1) if LifeChoice == 1 then self:GoToParent() repeat wait() until self.Occupied == false end self.Age = self.Age + 1 ----------- until self.Age == 5 self:Death() end function Life:Death() self.SelfMesh:Destroy() self = nil end return Life
All help is appreciated, thank you!
On this line:
game:GetService("TweenService"):Create(self.SelfMesh, TweenInfo.new(5), {Position = Vector3.new(self.SelfMesh.Position + Direction)}):Play()
You are constructing a new Vector3
with a Vector3
inside of it rather than trying to give it an actual position with numbers. Hence, it is interpreting it as position 0, 0, 0
, which is the center of the baseplate. Getting rid of the Vector3.new()
should fix this:
game:GetService("TweenService"):Create(self.SelfMesh, TweenInfo.new(5), {Position = self.SelfMesh.Position + Direction}):Play()
This solves your problem, but I see that Direction
is a unit vector, which means its size is only 1 stud, so it won't travel too far from self.SelfMesh.Position
. Just a heads up.
Additionally, I would like to suggest making use of the .Completed
event of Tween
rather than explicitly waiting out the length of the tween. Something like this should suffice:
local tween = game:GetService("TweenService"):Create(self.SelfMesh, TweenInfo.new(5), {Position = self.SelfMesh.Position + Direction}) tween:Play() tween.Completed:Wait()