What do I want to achieve?
I want to make a system where by having a part touch a another part (witch is the only part that can control other parts that touch it) and then teleport it to a area when teleported to that area it sets a bool value to true then other parts that are teleported go into another area (all the areas are invisible parts inside a model with bool values in them)
What is the issue?
It works for teleporting the first brick to an area but when it try’s teleporting more it try’s to go into the same area as the first part
What solutions have I tried so far?
I have searched around on the developer hub and YouTube and the forums and I cannot find a solution
local Cargo = game.Workspace.Positions:FindFirstChild("CargoPos")
local Position = Cargo.Position
Part=script.Parent
Part.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Union") and Cargo.HoldingCargo.Value == false then
hit.Parent.Union.Position = Cargo.Position
Cargo.HoldingCargo.Value = true
print("Success")
else
print("Failed")
end
end)
Could someone tell me how to get around this or another way to do this, Thanks!
no just when the cargo touches that highlighted part it will teleport to one of the half invisible boxs (the 3 next to eachother) then the next will teleport to a different one
I know how to do all that its just when they teleport they try to all go to the same place and they dont go to another place if the other has a cargo already in it
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.Union.Position = CargoSpot.Position -- Reposition it
CargoSpot.HoldingCargo.Value = true -- Change the value
print("Success")
else
print("Failed")
end
end)
This code is better because it will automatically find the avaliable cargo spots, which means you can add as many cargo spots as you want and it will work. Also please rename the cargo “CargoPart” so the code can work properly.