You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want the script to make a new part named “Dungeon 1” with the variable “LevelNumber”, and every time the part is touched to add 1 to “LevelNumber”, so that each time a new part is created it is named “Dungeon 2”, “Dungeon 3”, and so on.
What is the issue? Include screenshots / videos if possible!
The problem is that I think when I clone the script it clones the script before the variable “LevelNumber” is changed, so that it basically discards the changes it made.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I think it’s something to do with the :Clone() function, maybe there is something I don’t know about (obviously)?
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
I want to know how I can clone the script including the variables.
-- This is an example Lua code block
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
Whenever you clone a script, its environment (a.k.a. the place LevelNumber was sitting in) gets reset. So that means each script gets its own LevelNumber, and each script starts with that variable equaling 1.
If you want that variable to be persistent across scripts, you can either set it as an attribute on the new script/door or create an IntValue instance under either of them. Once you update that and clone it, their new value will be retained.
e.g.
-- ...
local LevelNumber = script:GetAttribute("LevelNumber")
-- in the event connection,
script:SetAttribute("LevelNumber", LevelNumber + 1)
-- clone the door/script
A couple of nitpicks:
the while loop will be creating a bunch of doors in the same position if you wait long enough (because a Door.Touched connection will be created every 1/30th of a second, with each one creating a door), just remove it and keep the event connection inside.
A possibly cleaner way of doing this is with a recursive function that gets called when the door is created:
-- make sure this script is outside this Door
local Door = -- door instance
local LevelNumber = 1
local function connectDoor(door)
local conn
-- not using door.Touched:Wait() in case of call stack overflow?
conn = door.Touched:Connect(function()
LevelNumber = LevelNumber + 1
local newDoor = Door:Clone()
newDoor.Name = "Dungeon " .. LevelNumber
newDoor.Position = door.Position + Vector3.new(0, 0, 10)
newDoor.Parent = workspace
connectDoor(newDoor)
conn:Disconnect()
end)
end
local firstDoor = Door:Clone()
firstDoor.Name = "Dungeon 1"
firstDoor.Parent = workspace
connectDoor(firstDoor)
You don’t need to add the while true loop. If you only add Door.Touched it will be constantly checking already.
+
You should keep the script disabled at first then clone it and enable it.