So I have this game which consists of A LOT of doors, and I have them all in a Folder in workspace (check 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 , 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.