Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why isn't this CFrame follow nodes script working correctly?

I have this script that makes a part follow nodes in number order using CFrame, and it works great except for one thing. Once it gets to the final node, instead of going back to node 1, it goes to some random node and THEN goes to node 1. The code is here:

Functions = {}
local Coaster=script.Parent
local PointsGroup=Coaster.Points
local Points=Coaster.Points:GetChildren()
local DifferenceBetweenPoints={0}
local CurrentTrackNumber={0}
local CurrentTrack={PointsGroup:FindFirstChild("1")}
local PreviousTrack={PointsGroup:FindFirstChild(#Points)}
local CurrentPosition={0}

Functions.MoveCar = function(Car)
local	Train=script.Parent.Train
local	Speed=script.Parent.Speed.Value
	script.Parent.CurrentTrackNumber.Value=CurrentTrackNumber[1]
	if Speed>0 then
		DifferenceBetweenPoints[Car]=(CurrentTrack[Car].Position-PreviousTrack[Car].Position).magnitude
		CurrentPosition[Car]=CurrentPosition[Car]+Speed
		if CurrentPosition[Car]>=DifferenceBetweenPoints[Car] then
			if CurrentTrackNumber[Car]<#Points then
				CurrentTrackNumber[Car]=CurrentTrackNumber[Car]+1
				PreviousTrack[Car]=CurrentTrack[Car]
				CurrentTrack[Car]=PointsGroup:FindFirstChild(CurrentTrackNumber[Car])
				CurrentPosition[Car]=CurrentPosition[Car]-DifferenceBetweenPoints[Car]
			else
				CurrentTrackNumber[Car]=1
				CurrentTrack[Car]=Points[1]
				PreviousTrack[Car]=Points[#Points]
			end
				if CurrentTrackNumber[Car]==#Points then
				print("Currently on the last piece of track!")
				CurrentTrackNumber[Car] =1
				CurrentTrack[Car]=Points[1]
				PreviousTrack[Car]=Points[#Points]
				CurrentPosition[Car]=1
			end
		end
		Train:FindFirstChild("Car"..Car):SetPrimaryPartCFrame(PreviousTrack[Car].CFrame:Lerp(CurrentTrack[Car].CFrame,(CurrentPosition[Car]/DifferenceBetweenPoints[Car])))
	end
end
return Functions

I also published the game and made it uncopylocked so you can see for yourself how it works. Any help is appreciated! Stay safe and healthy.

Uncopylocked example game: https://www.roblox.com/games/4952063914/CFrame-Issue

1 answer

Keep in mind, I explained everything the best i could

So, this is my script that i used in an old game:

--Node script named 'AI'
local self = {}

function self:StartEnemy(source,enemy) -- this begins the enemy
 local stats = require(game.ReplicatedStorage.Stats) -- this is the stats at the bottom
 
 stats = stats[enemy]
 
 local obj = game.ReplicatedStorage.Assets.Movement[enemy]:Clone() -- do not change enemy here, only change the folder path
 obj.CFrame = source["StartingPoint"].CFrame -- change 'StartingPoint' to what your enemy/npc moves from

 obj.Parent = workspace
 


 for i=1,#source,1 do
	print(i) -- this prints which node the item is on
  local scf = obj.CFrame
  for j=0,1,stats.Speed*0.002 do
   obj.CFrame = scf:lerp(source[i].CFrame + Vector3.new(0,obj.Position.Y-source[i].Position.Y,0),j)
   wait(0.01)
end
	end
		end


return self

also use this script (make sure the above is a modulescript)

local enemyai = require(game.ServerScriptService.AI)

local source = {}

for _,n in pairs(workspace.Course.Paths:GetChildren()) do
	source[n.Tag.Value] = n
end

source["StartingPoint"] = workspace.YourStartPath -- replace 'YourStartPath' with what it is called


while wait() do
	spawn(function()
enemyai:StartEnemy(source,"e") -- replace 'e' with your enemy/spawn
end)
wait(0.21) -- this is the cooldown between spawns
end

Add another ModuleScript named: Stats

local self = {
Normal = {
	Speed = 10,
	Health = 10, -- this and strength is random garbage I would keep it though
	Strength = 1,	
}
}
return self

this script should work, if i am missing any variables, tell me either through this or my discord: VortexVDev#2286

6 comments

0

So this is for a part that follows invisible nodes using CFrame?

0

I added the best explanation i could

0

also this should work im using it in my game

0

use the scripts above

0

This isn't necessarily what I'm looking for, I'm just looking to edit the code I already have.

0

well im not that sure

This discussion is preserved as it was. New answers, comments, and votes are closed.