Part teleportation issue

still problerms 30char 30char 30char

What did you do? Send a screenshot.

image

local Positions = game.Workspace:WaitForChild("Positions") -- Get the positions definition

Hitbox = script.Parent -- Renamed Part to hitbox

function FindAvaliableCargoSpot() -- Uses a function instead of inbuilt
	local Test = false
	for i, v in pairs(Positions:GetChildren()) do -- Gets all cargo spots and returns one if there is an avaliable one
		if v.HoldingCargo.Value == false then -- Check if cargo spot is avaliable
			Test = true
			return v -- Return the cargo spot
		end
	end
	if Test == false then
		return nil -- Return nil as there is no avaliable cargo spots
	end
end

Hitbox.Touched:connect(function(hit)
	local CargoSpot = FindAvaliableCargoSpot()
	if hit.Parent:FindFirstChild("CargoPart") and CargoSpot ~= nil then -- Check if the part is a Cargo part and there is an avaliable spot
		hit.Parent.CargoPart.Position = CargoSpot.Position -- Reposition it
		CargoSpot.HoldingCargo.Value = true -- Change the value
		print("Success")
	else
		print("Failed")
	end
end)

Hey so I believe there are some errors in the code that was sent, You wrote hit.Parent.Union, But we were looking for hit.Parent.CargoSpot, Simple mistake, We all do it once in a while, but we arent even looking for that, 2nd error is that we are going into the parent and looking for CargoSpot, which is not very good as that wont teleport the part that hit the hitbox, but instead grab the first instance it can find of CargoPart in the parent.

Also this isn’t really a error, but you shoudn’t call FindAvaliableCargoSpot when you are not yet sure that you are going to be using what is sending, It can cause performance issues later on.

After looking through the code multiple times, here is the fixed version:

local Positions = game.Workspace:WaitForChild("Positions") -- Get the positions definition

local Hitbox = script.Parent -- Renamed Part to hitbox

function FindAvaliableCargoSpot() -- Uses a function instead of inbuilt
	local Found = nil
	for i, v in pairs(Positions:GetChildren()) do -- Gets all cargo spots and returns one if there is an avaliable one
		if v.HoldingCargo.Value == false then -- Check if cargo spot is avaliable
			Found = v
			break
		end
	end
task.wait() -- not needed
	return Found
end

Hitbox.Touched:connect(function(hit)
	if hit.Name =="CargoPart" then
	local CargoSpot = FindAvaliableCargoSpot()
if CargoSpot == nil then print('Failed to find cargo spot') return end
		hit.Position = CargoSpot.Position -- Reposition it
		CargoSpot.HoldingCargo.Value = true -- Change the value
		print("Success")
	else
		print("Failed")
	end
end)

Sorry my bad! I fixed in my last code answer.