I would like to create a script that detects whether a player has a part in their inventory when they touch a part to decide whether they can pass through it or not.
What is the issue?
I have got a script already, however the script I’m using checks for the tools upon the player joining the game. I would like to change this to check when they touch the part instead.
Here is the script I am using:
local ToolName = "TestTool"
local doorPart = game.Workspace.Part
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if Player.Backpack:FindFirstChild(ToolName) then
doorPart.CanCollide = false
print("Player has tool in backpack!")
else
print("Player does not have tool in backpack!")
doorPart.CanCollide = true
end
end)
end)
script.Parent.Touched:Connent(function(hit)
local parent = hit.Parent
local player = game.Players:FindFirstChild(parent.Name)
if player then
local tool = player.Backpack:FindFirstChild(toolName)
if tool then
-- code to be executed if player has tool
end
end
end)
Quick remake of your script (tools disappear from Backpack when you equip them):
local ToolName = "TestTool"
local doorPart = game:GetService("Workspace"):WaitForChild("Part")
doorPart.Touched:Connent(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
if player:WaitForChild("Backpack"):FindFirstChild(ToolName) then
doorPart.CanCollide = false
elseif player.Character:FindFirstChild(ToolName) and player.Character:FindFirstChild(ToolName):IsA("Tool") then
doorPart.CanCollide = false
end
end
end)
also something to note about yours as well is that it will probably need remote events since anyone would be able to touch it and close it while someone else is walking through the door also you wrote doorPart.CanCollide = false twice