Hi there, I’ve been trying to make my boat anchor when it gets to a certain point, it is definitely an easy script, for some reason I cannot put my mind to it. I’m trying to make it so the one boat that actually touches it anchors instead of all the children in the folder. It would be very appreciated if you help!
This is what I scripted for it:
local AnchorPoint = script.Parent
local Boat1 = game.workspace.Boats.Boat1
local Boat2 = game.workspace.Boats.Boat2
local Boat3 = game.workspace.Boats.Boat3
local Boat4 = game.workspace.Boats.Boat4
local Boat5 = game.workspace.Boats.Boat5
local function BoatLanded(Part)
local Parent = Part.Parent
if game.workspace.Boats.Boat1 then
Boat1:GetChildren().Anchored = true
else
end
if game.workspace.Boats.Boat2 then
Boat2:GetChildren().Anchored = true
else
end
if game.workspace.Boats.Boat3 then
Boat3:GetChildren().Anchored = true
else
end
if game.workspace.Boats.Boat4 then
Boat4:GetChildren().Anchored = true
else
end
if game.workspace.Boats.Boat5 then
Boat5:GetChildren().Anchored = true
else
end
end
For this you would have to use for i, v in pairs() loops. Instead of just trying to change the property on the entire table of children, you instead have to loop through them and individually set anchored to true
Code example (I slightly cleaned up your code too):
local anchorPoint = script.Parent
local Boats = game.workspace.Boats
local function boatLanded(Part)
if Part.Parent.Parent.Name ~= "Boats" then return end -- exits the function if the touching part is not a boat (not inside game.workspace.Boats
for i, v in pairs(Part.Parent:GetChildren()) do -- loops through every child of the boat (Part.Parent)
if v:IsA("BasePart") then -- checks if the child is a "BasePart" (Part, MeshPart, UnionOperation, ...)
v.Anchored = true -- sets anchored property
end
end
end
anchorPoint.Touched:Connect(boatLanded) -- connects function "boatLanded" when anchorPoint touched
Hi, sorry for late response. Didn’t realise how the boat was setup, entirely my bad for the assumption. You could switch line 5 to
if Part.Parent.Parent.Parent.Parent ~= "Boats" then return end
or to make the code slightly more readable just check for “BoatScript” with :FindFirstChild()
if not Part.Parent.Parent:FindFirstChild("BoatScript") then return end -- exits the function (return) if it does not find a child named "BoatScript" inside of the Part.Parent.Parent (Vehicle)
if not Part:IsDescendantOf(Boats) then return end -- exits the function (return) if "Part" is not a descendant of the Boats folder
There are also other methods that are technically more performant like utilizing BoolValue objects as a tag for the boat, but replacing line 5 with any of the options above should work perfectly fine with the boat system you currently use.
Quick note: the current function only anchors parts that are within the same Parent as the part that hit the Anchor Point, it will not anchor any other parts outside of (in this case) the “LifeBoat” model
I don’t recommend editing unions via script they are very buggy. I couldn’t change transparency of union using for loop and :IsA("UnionOperation") try @jaminz4 's code but put unions in workspace or try using if v:IsA("Part") or v:IsA("UnionOperation") then. If it’s unions fault replace them with meshes.
Edit 1
Maybe set a PrimaryPart of model and do Model.PrimaryPart.Anchored = true
Like @kol11kol said, its easier just to make each boat have a primarypart and then anchor it. looping through every part to anchor the entire boat is unnecessary if you just… primary part
also is this script local or server? because it should be server
local Boats = workspace:WaitForChild("Boats")
local AnchorPoint = script.Parent
AnchorPoint.Touched:Connect(function(hit)
if hit.Parent.Parent.Parent.Parent.Name ~= "Boats" then return end
for i,v in pairs(hit.Parent.Parent.Parent:GetDescendants()) do
if v:IsA("BasePart") then
v.Anchored = true
end
end
end)
i wouldve done “IsDescendantOf()” but i feel it might not return the boat model as im not 100% how the boats hierarchy is; for example if the seat hit the part before the rest, it wouldnt work.