How to put a script randomly in a MeshPart?

So I have a big amount of Models in my game, and each Model contains a MeshPart named “Door”.

I have 2 scripts, one that kicks you once you touch the wrong Door (MeshPart), and one teleports you within the game to another Part.

So I have those 2 scripts ready & working but I was wondering if it was possible to make it so that the game automatically randomly chooses ONLY ONE Door (MeshPart) and inserts the teleport script inside of it, while the rest of the Doors (MeshParts) the game should insert the kick script inside of them. How would I make something like that?

(Please explain thoroughly as I am a horrible scripter)

1 Like

You don’t need to move a script inside the model, you can handle each of them from one script in ServerScriptService.

Put all your Models inside a folder in the workspace, make sure they all have a different name, and handle the Touch functions through a For_Do loop.


You can delete the prints, it was to make sure it is working ^^

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")
			--Teleport
		elseif Player and Humanoid and Root and State == false then
			print("Kick")
			--Kick
		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

Hey there,

I’m knot sure if I understant your question corectly, but I’ll try to halp you.

So, you have a bunch of modelz, each containing a MeshPart named “Dor”.

You have a skript that kicks you once you toch the wrong Dor and a skript that teleportz you within the game to another Part.

You want to randamly choose only one Dor and insert the teleport skript inside of it, while the rest of the Dors should insert the kick skript inside of them.

If I understant your question correctly, you can do this by using the RandomIntegar function.

This function returnz a randam integar between the two numbres you pas in.

So, you can use this function to choose a randam Dor to insert the teleport skript into.

Here iz an example of how you could do this:

local doors = workspace:GetDescendants()

for _, door in ipairs(doors) do
	if door:IsA("MeshPart") and door.Name == "Door" then
		if RandomInteger(1, #doors) == 1 then
			-- Insert teleport script here
		else
			-- Insert kick script here
		end
	end
end

This code will loop through all descendants in Workspace, check if they are a MeshPart named “Dor” and then use the RandomIntegar function to choose a randam Dor to insert the teleport skript into.

I hope this halps!

The way I’d do it is:

  • Put every door inside a folder (in generation or already in workspace)
  • create a ServerScript that picks a random one with folder:GetChildren()[math.random(1,#folder:GetChildren())] (How it works is, it picks a random number in the avaliable indices and grabs the one it got)
  • create a for loop that puts the Kick script inside the doors, if it gets to the one we got in the last step, put the teleport script instead.

it should look roughly like this:

... --assuming you have a Folder, KickScript and TeleportScript variable.
local Children = Folder:GetChildren()
local pickedIndex = math.random(1,#Children)
local pickedDoor = Children[pickedIndex]

for _,door in pairs(Children) do
    if door ~= pickedDoor then
        local CloneKickScript = KickScript
        CloneKickScript.Parent = door
        CloneKickScript.Enabled = true
    else
        local CloneTeleportScript = TeleportScript
        CloneTeleportScript.Parent = door
        CloneTeleportScript.Enabled = true
    end
end

Hope that helps!

Edit: messed up the script while writing it, sorry!

You cant just use ChatGPT and mispell some of the words in order to get pass peoples suspicions.

RandomInteger does not exist in Luau (Language used by ROBLOX scripts), nor Lua. and by seeing your post history, most of your posts have the same issues.

Please delete the posts generated by ChatGPT as anyone can ask ChatGPT themselves.