Hello, so I’m trying to make a door that requires a player to have a specific tool to be allowed to go through the door. So far I have been able to make it so that when you receive the tool, it will let you pass. The only issue is when I reset in the game or if I drop the item, it still lets me pass even though I don’t have the tool. I will show you what I have so far:
This is a Local Script that I currently have in Starter GUI.
local Player = game.Players.LocalPlayer
local doorPart = game.Workspace.EntryDoor
function updateInv()
local tools = Player.Backpack:GetChildren("Tool") -- Gets all the tools
tools[#tools+1] = Player.Character:FindFirstChildOfClass('Tool') -- Gets the tools inside the character
for i,v in pairs(tools) do -- for all these tools
if v:IsA('Tool') then -- if it really is a tool
doorPart.CanCollide = false
end
end
end
updateInv()
Player:WaitForChild('Backpack') -- wait for the backpack
Player.Backpack.ChildAdded:connect(updateInv) -- call this function every time something is added to the backpack
Player.Backpack.ChildRemoved:connect(updateInv) -- call this function every time something is removed from the backpack
If you need any more information please let me know. Thank you!
You mentioned that the player must have a specific tool, but your script is checking if the player has any tool in order to open the door (CanCollide = false).
Your function is firing when the player’s backpack gets a tool inside of it or when is removed, but you are not handling when the player drops the tool from its hands, plus you are not setting back CanCollide = true when that happens. In your function it only has CanCollide = false, so you are never setting back to true
Where would I Put the CanCollide = True and to make it check a specific item would I replace the first 2 “tool” sections? And I thought ChildRemoved checks if it is removed any way.
That event is listening if the Tool is removed from Backpack, not when the player drops the Tool from its hands. So Backpack.ChildRemoved actually fires when the Player equips the Tool, cause the Tool got removed from the Backpack and placed inside the Character.
Sorry I dont get what you mean by that
There are other approaches in which you could achieve this. Even by just editing a little your script and checking if the name of the Tool is the one you want to open the door.
You could do it in a different way, like:
A script in Tool that when Tool is Activated, the Door gets open
It’s letting you pass through when you don’t have the tool because you aren’t setting the collision back to true.
Some nicer code that does work from my testing with it
-- // Services
local Players = game:GetService("Players")
-- // Variables
local Player = Players.LocalPlayer
local Backpack = Player:WaitForChild("Backpack")
local DoorPart = workspace:WaitForChild("EntryDoor")
-- // Functions
local function UpdateInventory()
-- Loop trough everything inside the backpack, if its a tool, update the door and return
for _, tool in pairs(Backpack:GetChildren()) do
if tool:IsA("Tool") then
DoorPart.CanCollide = false
return
end
end
-- Loop trough everything inside the character, if its a tool, update the door and return
for _, tool in pairs(Player.Character:GetChildren()) do
if tool:IsA("Tool") then
DoorPart.CanCollide = false
return
end
end
-- If no tools were found inside the backpack or character, update the door and return
DoorPart.CanCollide = true
return
end
-- // Connections
Player.CharacterAdded:Connect(UpdateInventory)
-- Backpack Events
Backpack.ChildAdded:Connect(UpdateInventory)
Backpack.ChildRemoved:Connect(UpdateInventory)
-- Character Events
Player.Character.ChildAdded:Connect(UpdateInventory)
Player.Character.ChildRemoved:Connect(UpdateInventory)