My track placement system isn't working!

Hi! So I was trying to make a track placement system, and right now, I’m making it so if something is intersecting it, it won’t place. However, it randomly triggers, even when nothing is blocking it:


As you can see, nothing is blocking it, but I am still getting warned. Here’s my code:

local tracks = game.ReplicatedStorage.Tracks
local buttons = {}
local currentPoint = workspace.Start.Hit.CFrame
local lastPlacedTrack

for i, v in ipairs(script.Parent:GetChildren()) do
	if v.ClassName == "TextButton" then 
		table.insert(buttons, v) 
	end
end

for i, button in ipairs(buttons) do
	button.Activated:Connect(function()
		local trackType = button.Text
		local trackToClone = tracks[trackType]
		
		-- The test track is to determine if something is intersecting with the new track that we want to place. 
		-- If there is, don't place the track
		local testTrack = trackToClone:Clone()
		testTrack.Parent = game.Workspace
		testTrack:PivotTo(currentPoint)
		local collisions = testTrack.TrackModel:GetTouchingParts()
		local isBlocked = false
		
		-- Loop through every part touching it. If it's name is TrackModel, check if it was the last placed track. 
		-- If it isn't, set IsBlocked to true.
		for i, part in collisions do
			if part.Name == "TrackModel" then 
				if part ~= lastPlacedTrack then
					isBlocked = true
				end
			end
		end
		
		testTrack:Destroy()
		
		if isBlocked == true then 
			warn("Tracks cannot intersect") 
		else
			local newTrack = trackToClone:Clone()
			newTrack.Parent = game.Workspace
			newTrack:PivotTo(currentPoint) 
			currentPoint = newTrack.EndPos.CFrame 
			lastPlacedTrack = newTrack
		end
	end)
end

The reason I check if it’s name is TrackModel is because the tracks look like this:
Screenshot 2024-09-06 at 3.47.53 PM

If anyone knows a solution, please tell me!

1 Like

Post bump (I need help can someone respond)

Can someone please respond?! I need help…

The only thing I can think of, is that each collision is the parts that are colliding with your TrackModel part. You need to test if the part.Parent is the last track model, not that part in collisions.

for i, part in collisions do
    if part.Name == "TrackModel" then
        print(part.Parent)  -- Just to see what the output gives you
        if part.Parent ~= lastPlacedTrack then
            isBlocked = true
        end
    end
end
if part.Parent ~= lastPlacedTrack then

OHHHH! That was the problem! Thank you! I was checking if the part itself was the lastPlacedTrack, not its parent.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.