How to make a door that only opens once you touch five models

bassicly the title

1 Like

Using a LocalScript, you can do something like this:

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local Character = Players.LocalPlayer.Character
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Door = workspace.Door -- Replace with the name of your door model/part
local ModelsTouched = 0
local Connection

local DoorOpenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, 0, false, 0)
local DoorOpenGoals = {CFrame = CFrame.Angles(0 ,1, 0)}
local DoorOpenTween = TweenService:Create{Door, DoorOpenInfo, DoorOpenGoals} 
-- Replace with an animation if you prefer that rather than TweenService
	
local function GetTouchingParts()
	for i, v in ipairs(workspace:GetTouchingParts(HumanoidRootPart)) do
		if v:IsA("BasePart") and v.Parent.Name == ("TouchableModel") and v.Parent ~= Character and v.CanCollide then
			ModelsTouched = ModelsTouched + 1
			v.CanCollide = false 
			-- This is to prevent the player touching the same model multiple times
			if ModelsTouched == 5 then
				DoorOpenTween:Play()
			end
			Connection:Disconnect()
		end
	end
end

Connection = HumanoidRootPart.Touched:Connect(GetTouchingParts)

As long as you name the model “TouchableModel” this will work - also should add that the HumanoidRootPart may not work in this case, using a different bodypart such as the head may be better.

I haven’t tested this myself so if it throws any errors or doesn’t work as you want it to, please say.
Hope this helps!

2 Likes

thx ill try that when i have time

1 Like

Are you trying to make a door that only opens once each time the models are touched, or does it need to have a little bit of time when closing again, before you need to touch the models again?

it will stay open forever once you get all the models

were do i put the local script?

Put the LocalScript in StarterPlayer.StarterCharacterScripts

ill give you more context
im making a game kinda like mario 64 or odessey and i want to make a portal that teleports you to the second world but i want it so that you have to collect all of the start to open the portal

i already made the teleport script

Can you send the teleport script and where it is located in the explorer? The best way to do this is probably to integrate the two scripts somehow but I won’t know how without the script you wrote

local telePart = script.Parent

local TeleportService = game:GetService(‘TeleportService’)

local placeID = 8996667871

local canTeleport = true

local function otherGame(otherPart)

local player = game.Players:FindFirstChild(otherPart.Parent.Name)

if player and canTeleport then

	canTeleport = false

	TeleportService:Teleport(placeID, player)

end

end

telePart.Touched:Connect(otherGame)

the script is located in the portal

idk why it only lua blocked a little bit lol

You can probably have the portal part behind the door and then when the door opens you can just walk into the portal, that is unless you want to teleport instantly

As a failsafe, you could put them in the same script and only set canTeleport as true when all 5 models are touched.

ok i dont know anything about coding lol but is it a problem that the portal is a mesh?

MeshParts behave physically the same as a Part, the only difference is that they can be given a MeshId which makes their appearance that of a 3D model (usually made in Blender). If you want to read more, it’s best to look at Roblox’s own documentation here.

tl:dr no, it doesn’t matter

ok
i did make it in blender

1 Like

Just checking, does everything work now? The door opens properly and doesn’t give any errors? Just saying because, as mentioned before, I haven’t actually tested that code myself.

one thing should i put both the teleport and the other script in the portal?

also what do i name the collectibles?

Put the teleport script under the portal and the door script in StarterCharacterScripts, alternatively you can combine them and put the singular script under StarterCharacterScripts. For simplicities sake, it’s probably easier to just do the first thing

Name the collectables TouchableModel in order to work with the script above, however you can name them whatever you want as long as you change the v.Parent.Name == ("TouchableModel") to the name of the models.

and then do i name the portal door?