I wanted to make randomly generating tiles,with the spawn and the exit only generating one time,but when i try running the generation i get an error saying:
“The Parent property of Spawn/Exit is locked, current parent: NULL, new parent Workspace”
How can i fix that?
Heres the script:
local Placeholders = game.Workspace.PlaceHolders
local rooms = game:GetService("ReplicatedStorage"):WaitForChild("Setpieces"):GetChildren()
local HasSpawn = false
local HasExit = false
task.wait(5)
for i, placeHolder in pairs(Placeholders:GetChildren()) do
local chosenroom = rooms[math.random(1,#rooms)]:Clone()
if chosenroom.Name == "Spawn" then -- Spawn
if HasSpawn == false then
HasSpawn = true
chosenroom.PrimaryPart = chosenroom:WaitForChild("Carpet")
chosenroom:SetPrimaryPartCFrame(placeHolder.CFrame)
else
chosenroom:Destroy()
repeat
local chosenroom = rooms[math.random(1,#rooms)]:Clone()
if chosenroom.Name == "Spawn" then
chosenroom:Destroy()
end
until chosenroom.Name ~= "Spawn"
end
elseif chosenroom.Name == "Exit" then -- Exit
if HasExit == false then
HasExit = true
chosenroom.PrimaryPart = chosenroom:WaitForChild("Carpet")
chosenroom:SetPrimaryPartCFrame(placeHolder.CFrame)
else
chosenroom:Destroy()
repeat
local chosenroom = rooms[math.random(1,#rooms)]:Clone()
if chosenroom.Name == "Exit" then
chosenroom:Destroy()
end
until chosenroom.Name ~= "Exit"
end
else -- Normal room
chosenroom.PrimaryPart = chosenroom:WaitForChild("Carpet")
chosenroom:SetPrimaryPartCFrame(placeHolder.CFrame)
end
placeHolder:Destroy()
chosenroom.Parent = workspace -- Error here
end
Try this instead, you accidentally made a new variable instead of setting the value of a previous one:
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local Rooms = ReplicatedStorage.Setpieces:GetChildren()
local Placeholders = workspace.PlaceHolders
--//Controls
local HasSpawn = false
local HasExit = false
--//Initialization
task.wait(5)
for i, placeHolder in pairs(Placeholders:GetChildren()) do
local chosenroom = Rooms[Random.new():NextInteger(1, #Rooms)]:Clone()
if chosenroom.Name == "Spawn" then -- Spawn
if not HasSpawn then
HasSpawn = true
chosenroom.PrimaryPart = chosenroom:WaitForChild("Carpet")
chosenroom:PivotTo(placeHolder.CFrame)
else
chosenroom:Destroy()
repeat
chosenroom = Rooms[Random.new():NextInteger(1, #Rooms)]:Clone()
if chosenroom.Name == "Spawn" then
chosenroom:Destroy()
end
until chosenroom.Name ~= "Spawn"
end
elseif chosenroom.Name == "Exit" then -- Exit
if not HasExit then
HasExit = true
chosenroom.PrimaryPart = chosenroom:WaitForChild("Carpet")
chosenroom:PivotTo(placeHolder.CFrame)
else
chosenroom:Destroy()
repeat
chosenroom = Rooms[Random.new():NextInteger(1, #Rooms)]:Clone()
if chosenroom.Name == "Exit" then
chosenroom:Destroy()
end
until chosenroom.Name ~= "Exit"
end
else -- Normal room
chosenroom.PrimaryPart = chosenroom:WaitForChild("Carpet")
chosenroom:PivotTo(placeHolder.CFrame)
end
placeHolder:Destroy()
if not chosenroom then
warn("Chosen room could not be found.")
return
end
chosenroom.Parent = workspace
end