Repair module script

where did I go wrong? the error is below in case it helps

1 Like

You cannot set the parent of an Instance that has been destroyed.

2 Likes

In terms of options you could either:
A: parent the part to some container (like ReplicatedStorage) so you can reuse it.
B: create a new clone so that you can safely destroy.

B would look like this:

function DayNight.Day()
    NewPart:Destroy()
    NewPart = Part:Clone()
    Lighting.ClockTime = DayTime
end
1 Like

my idea was for the clone to appear in the workspace at night and to be destroyed during the day, the parent of the clone being in ReplicatedStorage. but as you can see, it doesn’t work

1 Like

If I see and understand it correctly, the solution is pretty simple and stupid :sweat_smile:
Change workspace to Workspace :joy:

1 Like

What @D1CEL said was correct, so this module should work for you:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")

local part = ReplicatedStorage.Part

local partClone = nil

local DayNight = {}

function DayNight.Day()
	if partClone and partClone.Parent then partClone:Destroy() end

	Lighting.ClockTime = 13
end

function DayNight.Night()
	partClone = part:Clone()
	partClone.Position = Vector3.new(4, 3, 4)
	partClone.Parent = workspace

	Lighting.ClockTime = 0
end

return DayNight

There’s even a way you can do this that doesn’t require the need to clone the part at all like so:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")

local part = ReplicatedStorage.Part

local DayNight = {}

function DayNight.Day()
	part.Parent = ReplicatedStorage

	Lighting.ClockTime = 13
end

function DayNight.Night()
	part.Position = Vector3.new(4, 3, 4)
	part.Parent = workspace

	Lighting.ClockTime = 0
end

return DayNight
1 Like

very helpful, but I have a question, how can I modify the script in such a way that it puts a random part in the workspace?

1 Like

Sure thing:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")

local partFolder = ReplicatedStorage.Parts

local part = nil

local DayNight = {}

function DayNight.Day()
	if part then
		part.Parent = partFolder
		part = nil
	end

	Lighting.ClockTime = 13
end

function DayNight.Night()
	local partTable = partFolder:GetChildren()

	part = partTable[math.random(#partTable)]
	part.Position = Vector3.new(4, 3, 4)
	part.Parent = workspace

	Lighting.ClockTime = 0
end

return DayNight

Do note that for this version to work properly, you’ll need to place the Parts inside of a Folder named “Parts” which is inside of ReplicatedStorage

explain what is different about the DayNight.Day() function, that is, about everything that happens in that function? that it is done differently than before

We need to store the part that’s currently in workspace in a variable to be able to easily parent it back to ReplicatedStorage. Since I don’t know which function you’ll be activating first (and you might change the order in the future), there’s a chance that the function will try to change the parent of nil which is why I added the if statement as a protection

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.