Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why doesn't this unanchor all parts it touches?

Asked by 7 years ago
Edited 7 years ago

So this is a serverscript inside of a part I have moving forward. The part is anchored and cancollide true. It should make every part it touches cancollide false and unanchored.

local p = script.Parent

p.Touched:connect(function(obj)
    if obj.ClassName == "Part" and obj.Anchored == true then
        obj.Anchored = false
        obj.CanCollide = false
    end
end)


0
Why are you searching for a Humanoid, and why are you trying to affect the part's CFrame? Discern 1007 — 7y
0
Wait showed wrong code hold on CheekySquid 78 — 7y
0
There's the actual code Im really sorry CheekySquid 78 — 7y
0
The script worked perfectly fine when I tested it in a game with a Baseplate and another part... Check to make sure the Disabled property of the script is unchecked, and make sure the script's parent is in fact the part that you want the parts it touches to be not Anchored and not Cancollide. Discern 1007 — 7y

3 answers

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

"So this is a serverscript inside of a part I have moving forward. "

You're most likely moving it forward with CFrame (correct me if i'm wrong). Well, as the wiki says, the Touched event won't fire if you're moving the part through something with CFrame. This is probably your problem.


You can use the method GetTouchingParts instead. This will return a table of all the parts that intersect with the part you called it on. It won't work for adjacent parts, only ones that intersect. It will also only work for parts that are CanCollide true.

Unfortunately, this isn't an event, so you'll have to use it in a loop. The best place would probably just be right after you move the part. Check out this simple script i just wrote:

local part = script.Parent

while wait() do
    part.CFrame = part.CFrame * CFrame.new(-0.1, 0, 0)
    for i, hit in ipairs( part:GetTouchingParts() ) do
        print(hit)
    end
end
0
I'm sure this is it I can't test t out as I'm not able to access a computer but I think this is it thanks! CheekySquid 78 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

It could be that the parts you want to be unanchored/not CanCollide are not actually anchored, in which case anchoring them or removing the and obj.Anchored == true will fix the problem. Also, you may wish to use the IsA function instead of the ClassName property, like so:

local p = script.Parent

p.Touched:connect(function(obj)
    if obj:IsA("BasePart") then
        obj.Anchored = false
        obj.CanCollide = false
    end
end)

This will work with other types of parts, such as Wedges, Trusses and Unions.

Log in to vote
-1
Answered by 7 years ago

i put it in a script inside a brick and it made all the parts that i had do that, even the baseplate, so i was just falling through over and over

0
Please use the comment feature unless you're giving a legitimate answer. Perci1 4988 — 7y
0
Please keep your words to yourself when I couldnt post a comment on it in the first place. User#12356 0 — 7y

Answer this question