Turning part collisions on and off on tool equip

i need help fixing my current script as it currently doesnt work as intended

  1. What do you want to achieve?

I wanna be able to phase through certain parts on my map when i have a certain tool equipped/held

  1. What is the issue?

with the scripts ive made either
A: i can walk through the target parts with and without the tool equipped
or
B: i cant go through the target parts at all

  1. What solutions have you tried so far?

ive tried turning collisions off and on via scripts on the tools and tried searching on the dev forum for help but nobody seems to had have a problem like mine

script (script is inside the tool itself)

Players = game:GetService("Players")
Tool = script.Parent
Handle = Tool:WaitForChild("Handle")


if Tool.Equipped == true then
	workspace.Door.CanCollide = false
	workspace.Door.Transparency = 0.5



if Tool.Equipped == false then
	workspace.Door.CanCollide = true
	workspace.Door.Transparency = 0

end
end

First of all, why are both your ends at the bottom of the script?
Second of all, why are all your variables global?

Tools have an event called .Equipped which fire when the player equips the tool and another event called .Unequipped which fires when the player unequips the tool.
Try seeing if this works:

local Players = game:GetService("Players")
local tool: Tool = script.Parent
local handle = tool:WaitForChild("Handle")

tool.Equipped:Connect(function()
	workspace.Door.CanCollide = false
	workspace.Door.Transparency = 0.5
end)

tool.Unequipped:Connect(function()
	workspace.Door.CanCollide = true
	workspace.Door.Transparency = 0
end)
1 Like

wow that works perfectly! ty!

also im basically a beginner when it comes to scripting so yea :cold_face:

2 Likes

All good! I recommend learning what is meant by local keyword so you can remember for the future :slight_smile:

Here’s the documentation to help you get started:

I wish you well for your future endeavours :fist:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.