Hello! I’m Clara, and I’ve been trying to make it to where whenever you touch a part (when you have a tool either in your backpack or equipped), the touched part’s transparency goes to 0 and the CanCollide is set to false, then the tool gets destroyed.
The problem I’ve been experiencing is that when the tool is in the backpack it works, but when it’s not in the backpack it doesn’t work.
I’ve already tried to look for solutions on the forum and I haven’t found any or I just didn’t search hard enough.
Anyways, this is my code (a server script that’s in the part):
local debounce = false
local part = script.Parent
local Players = game:GetService("Players")
part.Touched:Connect(function(hit)
if debounce == false then
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
local c = hit.Parent
local player = Players:GetPlayerFromCharacter(c)
if player.Backpack.Key1 then
print("at if")
part.CanCollide = false
part.Transparency = 0
player.Backpack.Key1:Destroy()
elseif c.Key1 then
print("at elseif")
part.CanCollide = false
part.Transparency = 0
c.Key1:Destroy()
else
print("at else")
end
wait(5)
debounce = false
end
end
end)
local debounce = false
local part = script.Parent
local Players = game:GetService("Players")
part.Touched:Connect(function(hit)
if debounce == false then
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
local c = hit.Parent
local player = Players:GetPlayerFromCharacter(c)
if player.Backpack:FindFirstChild("Key1") then
print("at if")
part.CanCollide = false
part.Transparency = 0
player.Backpack.Key1:Destroy()
elseif c:FindFirstChild("Key1") then
print("at elseif")
part.CanCollide = false
part.Transparency = 0
c.Key1:Destroy()
else
print("at else")
end
wait(5)
debounce = false
end
end
end)
local part = script.Parent
local Players = game:GetService("Players")
part.Touched:Connect(function(hit)
if debounce == false then
debounce = true
print(hit,hit.Parent)
if hit.Parent:FindFirstChild("Humanoid") then
print('ok')
local c = hit.Parent
local player = Players:GetPlayerFromCharacter(c)
if player.Backpack:FindFirstChild("Key1") then
print("at if")
part.CanCollide = false
part.Transparency = 0
player.Backpack.Key1:Destroy()
elseif c:FindFirstChild("Key1") then
print("at elseif")
part.CanCollide = false
part.Transparency = 0
c.Key1:Destroy()
else
print("at else")
end
wait(5)
end
debounce = false
end
end)