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

Why does this script not make my entire tool collidable?

Asked by
TechModel 118
3 years ago
Edited 3 years ago

I have this script parented with a tool. In the tool there are other scripts and models in models. This script doesnt affect the model ones, but why?

local tool = script.Parent
local part = tool:WaitForChild("Handle")

tool.Equipped:Connect(function()
    part.CanCollide = false 
    print("Equipped and removed collisions!")
end)

tool.Unequipped:Connect(function()
    part.CanCollide = true
    print("Equipped and added collisions!")
end)

What I would like is for the tool to be completely not collidable, but if it is unequipped or dropped, then of course I want it to be collidable so i doesn't swing on the floor or fall through.

0
I changed my script, sorry for the bug, I didnt realise that Xapelize 2658 — 3y

1 answer

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

You need to use an pairs loop for it. Pairs loop will help you to do the job for making instances in a table a loop! For example:

  • Making a model's every children CanCollide false
  • Name every brick in the workspace to "Hello"
  • More......

For how to use it, heres an example:

for index, instance in pairs(workspace:GetChildren()) do
    print(index)
    print(instance.Name)
end

Lets explain first from the first line:

the first word, for is a template of pairs loop, you must type it in

the second word, index, what's index? It's how many times the pairs loop looped through in a table! For example, there is two parts on the workspace that are both named Part, it WILL print this when you play test it:

1
Part
2
Part

What about 10 parts on the workspace that are all named Cheese? Well they result in the same logic:

1 
Cheese
2
Cheese
3
Cheese
4
Cheese
5
Cheese
6
Cheese
7
Cheese
8
Cheese
9
Cheese
10
Cheese

Is index important? It depends on the situation, well in this situation, we won't use it

You can directly ignores the index, you will never let him have a name, and you cannot index it! That helps you clean for a bit and more choices of keywords in the same function.

for _, instance in pairs(workspace:GetChildren()) do
    print(_) -- You cannot index it, so this will give an error
    print(instance.Name)
end

Now for the third keyword, instance, is the instance that the pairs loop is looping on! It's pretty self explanatory from the example here

Now for the third keyword, instance, is the instance that the pairs loop is looping on! It's pretty self explanatory from the example above

Is instance ok to be ignored? Yes, it won't give an error, but it totally ruins the point of pairing since you CAN also do the same thing with for loop. I won't give an example here, but just don't ignore the instance.

You also can change the name of index and instance, you can name any of them dasojkads or afcvifjasjfas, they don't matter as long as YOU DON'T OVERLAP THE SAME VARIABLE NAME, also don't name them random words, that doesn't help you lol

What about the thing inside the pairs loop bracket?

If you are confused, the code inside the pairs loop bracket is workspace:GetChildren(), which you can find a tutorial here; or something longer, https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren

if you are really scared to go to these links, search it on google about GetChildren and the first result is the same thing of what I gave.

GetChildren returns table (or array, i find array confusing since it's a staff name from some games, so i will use table on the rest of the topic), also you can only put tables inside the bracket

and the rest are template codes

In your situation, you need to collide false an entire model, which need pairs loop:

for _, instance in pairs(model:GetChildren()) do -- Ignore the index
    instance.CanCollide = false -- Make the instance CanCollide false
end

What's is the instance? Look at the place where i explain it

Anyway, that's a long topic but skipping to the point: heres the script and it's logic

local tool = script.Parent
local part = tool:WaitForChild("Handle")

tool.Equipped:Connect(function()
    for _, instance in pairs(part.Parent:GetChildren()) do -- pairs loop
        instance.CanCollide = false -- can collide false
    end
    print("Equipped and removed collisions!")
end)

tool.Unequipped:Connect(function()
    for _, instance in pairs(part.Parent:GetChildren()) do
        instance.CanCollide = true -- can collide true
    end
    print("Equipped and added collisions!")
end)

if you really read the explanation you might understand a little, check back soon or bookmark this page if you think it's very hard and really confusing, maybe you will understand someday bye!!

0
my hands hurts Xapelize 2658 — 3y
0
I appreciate the detailed explanation, but on line 6 the output says "CanCollide is not a member of Script" and also lone 13 TechModel 118 — 3y
Ad

Answer this question