How do I make this server script insert a specific script in one part?

So I have this game which consists of A LOT of doors, and I have them all in a Folder in workspace (check image):

image

Now I want the script to randomly insert a script ONLY IN ONE Door (the MeshPart named Door) that teleports the player to a part in workspace called “Tele2”, also I want it to print out which door it chose (the numbered models called Entrance as seen in the image) so that I can view in Output which one it was and ensure it works when testing it.

I also wanted it to insert a script in THE REST of the doors that includes a kick script with a custom message so that if you go through the wrong door you get kicked out of the game.

Here is the current server script I have (in ServerScriptService) which I have 0 idea how to fix :face_with_spiral_eyes: , so please let me know if you are able to help!

local PlayerService = game:GetService("Players")

local DoorFolder = workspace:WaitForChild("Doors",300)
local Child = DoorFolder:GetChildren()
local RandomDoor = math.random(1, #Child)

print(RandomDoor)

local function Touched(DoorModel, State)
	DoorModel.Door.Touched:Connect(function(Hit)
		local Character = Hit.Parent
		local Player: Player = PlayerService:GetPlayerFromCharacter(Character)

		local Humanoid = Character:FindFirstChild("Humanoid")
		local Root = Character:FindFirstChild("HumanoidRootPart")

		if Player and Humanoid and Root and State == true then
			print("Teleport")
			
				local function onTouch(part)
					h = part.Parent:FindFirstChild("Humanoid")
					if (h~=nil) then
						h.Parent:MoveTo(script.Parent.Parent.Tele2.Position)
					end
				end
				script.Parent.Touched:connect(onTouch)
			
		elseif Player and Humanoid and Root and State == false then
			print("Kick")
			
				script.Parent.Touched:Connect(function(hit)
					local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
					plr:Kick("Try again... :-)")
				end)

		end
	end)
end

for _,DoorModel in pairs(DoorFolder:GetChildren())do
	if DoorModel:IsA("Model")and DoorModel.Name == "Door".. RandomDoor then
		Touched(DoorModel, true)
	elseif DoorModel:IsA("Model")and not (DoorModel.Name == "Door".. RandomDoor) then
		Touched(DoorModel, false)
	end
end

Note: The game is a 1-player-server type of game, I’m not sure if that helps but I thought of saying it just incase.

Make the script, disable it and place it in the spawner script. When you want to put it under it, you can clone the script and enable it.

1 Like

Do you mind elaborating more, please?

Apologies, I am a really bad scripter.

Make the script like you normally would as if it were placed under the part to begin with. Then in the properties tab, uncheck the Enabled property and place it in your spawner script (that gets the random part). Then when you want the script to be inserted, clone it and parent it to the part, and then set the Enabled property back to true in the spawner script.