Help with door script and random generation

Hello,
I’m making a game where certain rooms (put in a folder) are generated using a module and a generation script in SeverScriptService, The key will be put in a model in the folder and that the script will then generate the model.
My problem is that my key script once used on a door with a similar name will open ever single door or only open one and then not open any other door. How can I change this?

The Key Code

local key = script.Parent
local doormodel = game.Workspace.LibaryDoorModel_Bronze
local Ldoor = doormodel.Left_Door
local Rdoor = doormodel.Right_Door
local keyhole = doormodel.KeyHole
local sound = doormodel.Unlock_Sound
local value = doormodel.Value.Value


local keyinfo = TweenInfo.new(
	3.5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	-1,
	true,
	0
)
local doorinfo = TweenInfo.new(
	4,
	Enum.EasingStyle.Bounce,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local Ldoortween = tweenservice:Create(Ldoor, doorinfo, {CFrame = doormodel.Left_Door_Open.CFrame})
local Rdoortween = tweenservice:Create(Rdoor, doorinfo, {CFrame = doormodel.Right_Door_Open.CFrame})
local spin = tweenservice:Create(script.Parent,keyinfo,{CFrame = script.Parent.CFrame * CFrame.Angles(math.rad(0),math.rad(-180),0)})


local NewPosition = key.Position + Vector3.new(0, 1, 0)

local bounce = tweenservice:Create(key, keyinfo, {Position = NewPosition})

spin:Play()
bounce:Play()

key.Touched:Connect(function(open)
	if key.Color == value then
		sound:Play()
		Ldoortween:Play()
		Rdoortween:Play()
		key:Destroy()
		keyhole.Anchored = false
		wait(2)
		keyhole.Anchored = true
	end

end)
Generation Module
room.random = Random.new()

function room.Generate(prevRoom)
	local possibleRooms = workspace.Rooms:GetChildren()
	local randomRoom = possibleRooms[room.random:NextInteger(5, #possibleRooms)]
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	newRoom.Entrance.Transparency = 1
	newRoom.Exit.Transparency = 1
	newRoom.Entrance.CanCollide = false
	newRoom.Exit.CanCollide = false 
	
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

return room

Room Script

local prevRoom = workspace.StartRoom


for i=1, 1 do
	prevRoom = room.Generate(prevRoom)
end

If you need any more info please ask me, and if it is complexted could you attach some documentation

And no im not asking for you to code for me im just asking for help

Thank you :sweat_smile:

2 Likes

Whenever your key touches anything it will only check if the key is the same color as the assigned door which is always true, but you need use the hitpart parameter which for some reason is named “open” in your script, but the hitpart is any part inside your doormodel so you could use hitpart:FindFirstAncestorWhichIsA(“Model”) and check if that is the same model as your doormodel using an if statement

1 Like