Door script not working

Hello. im making a wardrobe where u can hide in like the game Doors and when i tried making it it won’t run the script that fires the remote event that will make the wardrobe function

here is my script. sorry if i explained it badly. i have put a note where it is it won’t run

local TS = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local character = player.Character
local hrp = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")

local function GetTouchingParts(part)
	local connection = part.Touched:Connect(function() end)
	local results = part:GetTouchingParts()
	connection:Disconnect()
	return results
end

-- WARDROBE
UIS.InputBegan:Connect(function(input,gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.E then
		local enter_locker = false
		
		for i,lockers in pairs(GetTouchingParts(hrp)) do -- here it won't run
			if lockers.Name == "Hitbox" and lockers.Parent.Name == "Wardrobe" and lockers.Parent:FindFirstChild("Db") and lockers.Parent.Db.Value == false then
				game.ReplicatedStorage.Remotes:FindFirstChild("Wardrobe").Enter:FireServer(lockers.Parent)
				local enter_locker = true -- to here
			end
		end
		
		if not enter_locker then -- don't know if this will run as i haven't been able to test it because u can't go into the  locker
			for i,lockers in pairs(workspace.Interactables:GetChildren()) do
				if lockers.Name == "Wardrobe" and lockers.Occupant.Value == player and lockers:FindFirstChild("Db") and lockers.Db.Value == false then
					game.ReplicatedStorage.Remotes:FindFirstChild("Wardrobe").Exit:FireServer(lockers)
				end
			end
		end
	end
end)

help is very appreciated since this could save my game because i don’t really know how to make the closet script otherwise

1 Like

May I ask why you have a connection in GetTouchingParts()?

Have you tried printing the result of the function? It is possible that the function returns {} which would mean it won’t have anything to loop over, and therefore not run the code inside the for-loop

i just printed the results and it returned nil and the GetTouchingParts() thing i didn’t make but how would i be able to make it not return nil? so it will function?

GetTouchingParts must have a touch connection to track it’s value.

On that same note I think you will need the touched connection to exist before the player touches it, GetTouchingParts can’t track what has happened before it’s existance. Try creating this connection when the game starts and let it disconnect on it’s own when the door is destroyed or the game ends (which ever comes first)

You sure? Everytime I’ve used GetTouchingParts() it has returned correct values, only thing to worry about is CanTouch == true, which all parts have by default.

https://developer.roblox.com/en-us/api-reference/function/BasePart/GetTouchingParts

If the part itself has CanCollide set to false, then this function will return an empty table UNLESS it has a TouchInterest

You are right, I misunderstood this when reading. Maybe our author has CanCollide sets to false?

1 Like

so i will need to add a touchinterest? in the part

Only if CanCollide is set to false. Otherwise :GetTouchingParts will not need a connection to work.

alright my cancollide is set to false so i will add touch interest a bit later when i stopped playing with my friends thanks for the help i will text if it dosen’t work

Why not use a CanCollide Part that’s Transparent where you want the Player to touch, instead of GetTouchingParts? Seems more inefficient than just using a standard script to check the Touched property, then check if the Touched Part is a member of a Humanoid.
The TouchInterest will automatically get created when you have the Touched function in your script.