Can anyone patch this error

local event = game:GetService("ReplicatedStorage").Died
local event2 = game:GetService("ReplicatedStorage").Start
local latestTrack = nil
local latestObstacle = nil
local createTracks = false  -- Boolean variable to control track creation
local function deleteAllTracks()
	for i, track in pairs(workspace.Tracks:GetChildren()) do
		if i ~= 1 then
			track:Destroy()
			print("Destroyed tracks expect first.")
		end
	end
	for i, obstacle in pairs(workspace.Obstacles:GetChildren()) do
		if i ~= 1 then
			obstacle:Destroy()
			print("Destroyed obstacles expect first.")
		end
	end
end


event.OnServerEvent:Connect(function(player: Player) 
	deleteAllTracks()
	createTracks = false
end)

event2.OnServerEvent:Connect(function(player: Player)
	createTracks = true
end)

-- The main loop for creating new tracks
while task.wait(3) do
	if createTracks then
		print("New Track and New Obstacles")
		local newTrack = workspace.Tracks.TrackPart:Clone()
		local newObstacle = game.ServerStorage.Obstacles.Laser1:Clone()
	-- If latestTrack is not nil, set the position of the new track relative to the last track's position
	if latestTrack then
		newTrack.Position = Vector3.new(newTrack.Position.X, newTrack.Position.Y, latestTrack.Position.Z - 500)
		end
		if latestObstacle then
			newObstacle:PivotTo(CFrame.new(newObstacle.Laser.CFrame.X, newObstacle.Laser.CFrame.Y, latestObstacle.Laser.CFrame.Z - 250, 0, 0, 1, -1, 0, 0, 0, -1, 0))
		end
		newTrack.Parent = workspace.Tracks
		newObstacle.Parent = workspace.Obstacles
		latestTrack = newTrack
		latestObstacle = newObstacle
	end
end

So the problem in the code is that sometimes at line 42 it will say
Laser is not a valid member of Laser1

newObstacle:PivotTo(CFrame.new(newObstacle.Laser.CFrame.X, newObstacle.Laser.CFrame.Y, latestObstacle.Laser.CFrame.Z - 250, 0, 0, 1, -1, 0, 0, 0, -1, 0))
2 Likes

before doing that, you should first check if the laser exists by using a if statement, like if laser then – do action.

2 Likes

Ill try that ig

1 Like

try by doing (assuming newobstacle is the laser)

if NewObstacle then
-- code here
end
1 Like

newObstacle.Laser i think u mean that

1 Like

yeah, and also try doing :WaitForChild too!, as it may spawn before code execution.

3 Likes

yeah, ill do that, the script spawns obstacles each 3 sec, so ill do waitforchild, so that it waits for it to spawn

do and let me know if it works!

i will solution this reply when it works, u will receive notification from the solution

wait hold on:


how does that not even work it even went through the :waitforchild

I mean the error is pretty simple and clear, can you show us your workspace?


ik its clear but its not clear on why it is doing that

newObstacle:PivotTo(CFrame.new(newObstacle:WaitForChild("Laser").CFrame.X, newObstacle:WaitForChild("Laser").CFrame.Y, latestObstacle.Laser.CFrame.Z - 250, 0, 0, 1, -1, 0, 0, 0, -1, 0))

What is “latestObstacle”

the last second obstacle that has been added?
image
was a bit obvious tbh

Yeah but can you show me where it is in the script or the fully updated script

at the end of the code (the if statements ig)

newObstacle:PivotTo(CFrame.new(newObstacle:WaitForChild("Laser").CFrame.X, newObstacle:WaitForChild("Laser").CFrame.Y, newObstacle:WaitForChild("Laser").CFrame.Z - 250, 0, 0, 1, -1, 0, 0, 0, -1, 0))

send me the whole updated code and ill fix it

local event = game:GetService("ReplicatedStorage").Died
local event2 = game:GetService("ReplicatedStorage").Start
local latestTrack = nil
local latestObstacle = nil
local createTracks = false  -- Boolean variable to control track creation
local createObstacles = false

local function deleteAllTracks()
	for i, track in pairs(workspace.Tracks:GetChildren()) do
		if i ~= 1 then
			track:Destroy()
			print("Destroyed tracks expect first.")
		end
	end
	for i, obstacle in pairs(workspace.Obstacles:GetChildren()) do
		if i ~= 1 then
			obstacle:Destroy()
			print("Destroyed obstacles expect first.")
		end
	end
end


event.OnServerEvent:Connect(function(player: Player) 
	deleteAllTracks()
	createTracks = false
	createObstacles = false
end)

event2.OnServerEvent:Connect(function(player: Player)
	createTracks = true
	createObstacles = true
end)

-- The main loop for creating new tracks
while task.wait(3) do
	if createTracks then
		print("New Track and New Obstacles")
		local newTrack = workspace.Tracks.TrackPart:Clone()
		local newObstacle = game.ServerStorage.Obstacles.Laser1:Clone()
	-- If latestTrack is not nil, set the position of the new track relative to the last track's position
	if latestTrack then
		newTrack.Position = Vector3.new(newTrack.Position.X, newTrack.Position.Y, latestTrack.Position.Z - 500)
		end
		if latestObstacle then
			if newObstacle:WaitForChild("Laser") then
				newObstacle:PivotTo(CFrame.new(newObstacle.Laser.CFrame.X, newObstacle.Laser.CFrame.Y, latestObstacle.Laser.CFrame.Z - 250, 0, 0, 1, -1, 0, 0, 0, -1, 0))
			end
		end
		newTrack.Parent = workspace.Tracks
		newObstacle.Parent = workspace.Obstacles
		latestTrack = newTrack
		latestObstacle = newObstacle
	end
end

Did you try to set the parent first?

local event = game:GetService("ReplicatedStorage").Died
local event2 = game:GetService("ReplicatedStorage").Start
local latestTrack = nil
local latestObstacle = nil
local createTracks = false  -- Boolean variable to control track creation
local createObstacles = false

local function deleteAllTracks()
	for i, track in pairs(workspace.Tracks:GetChildren()) do
		if i ~= 1 then
			track:Destroy()
			print("Destroyed tracks expect first.")
		end
	end
	for i, obstacle in pairs(workspace.Obstacles:GetChildren()) do
		if i ~= 1 then
			obstacle:Destroy()
			print("Destroyed obstacles expect first.")
		end
	end
end


event.OnServerEvent:Connect(function(player: Player) 
	deleteAllTracks()
	createTracks = false
	createObstacles = false
end)

event2.OnServerEvent:Connect(function(player: Player)
	createTracks = true
	createObstacles = true
end)

-- The main loop for creating new tracks
while task.wait(3) do
	if createTracks then
newTrack.Parent = workspace.Tracks
		newObstacle.Parent = workspace.Obstacles
		latestTrack = newTrack
		latestObstacle = newObstacle

		print("New Track and New Obstacles")
		local newTrack = workspace.Tracks.TrackPart:Clone()
		local newObstacle = game.ServerStorage.Obstacles.Laser1:Clone()
	-- If latestTrack is not nil, set the position of the new track relative to the last track's position
	if latestTrack then
		newTrack.Position = Vector3.new(newTrack.Position.X, newTrack.Position.Y, latestTrack.Position.Z - 500)
		end
		if latestObstacle then
			if newObstacle:WaitForChild("Laser") then
				newObstacle:PivotTo(CFrame.new(newObstacle:WaitForChild("Laser").CFrame.X, newObstacle:WaitForChild("Laser").CFrame.Y, latestObstacle.Laser.CFrame.Z - 250, 0, 0, 1, -1, 0, 0, 0, -1, 0))
			end
		end
	end
end

try it