Hello, I have this script that I made for a door system in a game I’m making below.
local TweenService = game:GetService("TweenService")
for i, v in pairs(game.Workspace:GetDescendants()) do
if v:GetAttribute("Door") then
local open = false
local debounce = false
local debounce2 = false
local prompt = v.Main.DoorInteract
local hinge = v.Hinge
local currentCFrame = hinge.CFrame
local Sound = v.Main.DoorOpen
local goal
local Tweeninfo = TweenInfo.new(
0.6,
Enum.EasingStyle.Quad,
Enum.EasingDirection.Out
)
local function Interact()
if debounce == false then
debounce = true
if open == false then
open = true
goal = {CFrame = currentCFrame * CFrame.Angles(0, math.rad(90), 0)}
else
open = false
goal = {CFrame = currentCFrame * CFrame.Angles(0, math.rad(0), 0)}
end
local tween = TweenService:Create(hinge, Tweeninfo, goal)
tween:Play()
Sound:Play()
tween.Completed:Wait()
debounce = false
end
end
local function NPCInteract(Hit)
if Hit.Parent:WaitForChild("NPC", 0.1) and debounce2 == false then
debounce2 = true
if open == true then
v.Main.CanCollide = true
Interact()
wait(0.5)
debounce2 = false
elseif open == false then
v.Main.CanCollide = false
Interact()
wait(Tweeninfo.Time)
v.Main.CanCollide = true
Interact()
end
end
end
v.Main.Touched:Connect(NPCInteract)
prompt.Triggered:Connect(Interact)
end
end
the problem is with the NPCInteract function. it only sometimes does what it’s meant to. I noticed that sometimes the NPC tries to go through the door, but the door won’t open, and it just stands there, trying to get through the door. And as far as I can tell, no errors show up when it happens, It just stops working randomly.
I initially had the NPCInteract function part of a separate script for NPCs, and the same issue was present. But, I just inputted the function into the main door script, and nothing had changed about it.
Is there a better way to code door systems for NPCs and players? please let me know.
local Debounce = false
local Open = false
local function Interact(IsNPC)
if not Debounce then
Debounce = true
Open = not Open -- changes true to false, false to true
if Open then
print("Opening")
else
print("Closing")
end
task.wait(.5)
Debounce = false
if IsNPC and Open then Interact(true) end -- If NPC interacted and the door was left open, then close it
end
end
v.Main.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("NPC") then
Interact(true)
end
end)
prompt.Triggered:Connect(function()
Interact()
end)
Is the Touched event firing?
Try putting a print statement, something like print(Hit.Name) after your NPCInteract function starts.
How big is the Part that triggers the door opening? Is it just at the surface of the floor by the door, or is it a large CanCollide false Part in front of the door? Your issue may simply be that the touched event isn’t firing.
It also may be an item of the NPC that doesn’t pass the if Hit.Parent:WaitForChild("NPC, 0.1) statement like an Accessory.
I put a part on the door model which has a pathfinding modifier that says the door can be passed through, so it would try to go through the door but the door won’t open.
But where is the Part the Touched event fires from? If it’s only at the ‘front’ side of the door and the NPC can’t touch it from the back side then it won’t work from the back.
Also try this like I suggested before:
v.Main.Touched:Connect(function(Hit)
print(Hit.Name) -- will tell you WHAT fires the .Touched event so you can see if it can get Hit.Parent:FindFirstChild("NPC")
if Hit.Parent:FindFirstChild("NPC") then
Interact(true)
end
end)