Script not working as intended

Im currently working on a scary game, i’m doing a script that spawns monsters during the night and removes them during the day. I have no errors in the script, but for a reason, its not working as it is intended to. First of all, the monster are in a folder called “Monsters” in Replicated Storage, and are to be cloned in a folder called “Creatures” in workspace.

rep
rep2

So during night, the monsters in the replicated storage folder should be cloned in the folder in workspace. But for a reason, the folder get cloned (with the monsters and then put in the workspace folder, just like this.

rep3

And finally my other issue is with the monsters being removed during the day, in the script they spawn at 18 (start of night) and they are removed at 6 (start of day), but for a reason, when the ingame clock hits 0 (in between 18 and 6), the script decides to remove them and when the monster are supposed to be removed, it doesnt work.

If you could help me fix this, i would really appreciate it. Thanks for your time.
The script will be in an other reply.

1 Like
local light = game:service("Lighting") 
local MonstersHaveSpawned = false

while true do 
	wait(10)
	if light.ClockTime <= 6 then
		print("Monsters have been removed")
		game.Workspace.Creatures.ScaryBoy.Respawn.Enabled = false
		game.Workspace.Creatures:FindFirstChild("ScaryBoy"):Destroy()
		wait(.2)
		MonstersHaveSpawned = false
		
		-- wait(303)
		
		--game.ReplicatedStorage.Monsters:Clone().Parent = game.Workspace

	else
		if light.ClockTime >= 18 and MonstersHaveSpawned == false then
			print("Monsters Have Spawned")
			game.ReplicatedStorage.Monsters:Clone().Parent = game.Workspace.Creatures
			wait(.2)
			MonstersHaveSpawned = true
			--wait(303) 
		
		end


	end

end
1 Like

Monsters will be removed at 00 because your statement “if light.ClockTime <= 6”, 00 IS less than 6, so they’re removed. You can just change the comparator

if light.ClockTime >= 6 then…

You’re getting the Monster folder to show up in the Creatures folder because Cloning something clones that item and all it’s children, so you want to specify to clone all the children, not the folder.

use this in place of your current clone

for _, v in pairs(game.ReplicatedStorage.Monsters:GetChildren()) do
local clone = v:Clone()
clone.Parent = game.Workspace.Creatures
end

2 Likes

Luckily, an easy fix!

Like he said, make sure your clock time check is right. Your original <= 6 is backwards.

Additionally, you should also affirm its less then 18 at night and use your variable to see if they exist already. Here’s what that’ll look like:



if light.ClockTime >= 6 and light.ClockTime < 18 and MonstersHaveSpawned == true then
    MonstersHaveSpawned = false
    print("day")
elseif light.ClockTime >= 18 and MonstersHaveSpawned == false then
   MonstersHaveSpawned = true
   print("night")

Also, you’re cloning the entire monsters folder instead of the creature themselves.
game.ReplicatedStorage.Monsters:Clone().Parent = game.Workspace.Creatures
If you need to clone then individually, you can clone all the folder’s children like Odobenus said.

local creatures = workspace:WaitForChild("Creatures")
local replicated = game:GetService("ReplicatedStorage")

local Monsters = replicated.Monsters:GetChildren() -- get all monsters
for _, Monster in ipairs(Monsters) do -- for every monster
	local NewMonster = Monster:Clone()
	-- Monster.Respawn.Enabled = false -- edit them as needed
	Monster.Parent = creatures
end

With some additional code cleanup, here’s the working version. (also, task.wait() is just a minor preference)

-- Using GetService() and defining our workspace folder
local replicated = game:GetService("ReplicatedStorage")
local creatures = workspace:WaitForChild("Creatures")
local light = game:GetService("Lighting")
local MonstersHaveSpawned = false

-- Wait 5 seconds before looping
while task.wait(5) do 
	
	-- Check if clocktime is above or equal to 6, and is less then 18 at night
	-- If monsters have spawned, remove them, and set the variable to false.
	if light.ClockTime >= 6 and light.ClockTime < 18 and MonstersHaveSpawned == true then
		print("Monsters have been removed")
		MonstersHaveSpawned = false
		creatures:ClearAllChildren() -- Removes all monsters without searching manually				
		
	-- If monsters have not spawned, and it is more or equal to 18 at night, spawn them and set to true.
	elseif light.ClockTime >= 18 and MonstersHaveSpawned == false then
		MonstersHaveSpawned = true

		-- For every model inside the monsters folder, clone them
		-- and parent them to the creature folder we defined
		local Monsters = replicated.Monsters:GetChildren()
		for _, Monster in ipairs(Monsters) do
			local NewMonster = Monster:Clone()
			-- Monster.Respawn.Enabled = false
			Monster.Parent = creatures
		end
		
		print("Monsters Have Spawned")		
	end

end
2 Likes

Thank you for the help but theres still one new problem now, the monster for the replicated storage folder isnt there anymore after a night.

rep4

My mistake, I made a typo in the script.
Monster should’ve been NewMonster for the clone

		for _, Monster in ipairs(Monsters) do
			local NewMonster = Monster:Clone()
			-- NewMonster .Respawn.Enabled = false
			NewMonster.Parent = creatures
		end
2 Likes

ill try it and ill tell u if it works

1 Like

Thank you so much it worked, but theres one more thing i need help with, i have an other script that makes it foggy during the night and clear during the day, for a reason, the fog stays during the day and doesnt go away, could you help. Thanks

local nightSound = game.Workspace.nightSound -- Enter night sound location here
local daySound = game.Workspace.daySound --Enter day sound location here
local CustomMusic = game.Workspace.CustomMusic --Additional Music

while true do
	local Time = game.Lighting.TimeOfDay
	Time = (Time:gsub("[:\r]", ""))
	Time = tonumber(Time)

	if Time >= 180000 or Time < 60000 then
		--Between 1800hrs and 0600hrs


		wait(0.1)
		if daySound.Playing and CustomMusic.Playing then daySound:Stop()  end
		if not nightSound.Playing then
			nightSound:Play()
			game.Lighting.FogStart = 10 
			game.Lighting.FogEnd = 120 
			game.Lighting.FogColor = Color3.new(0,0,0)
		end

	else
		--Between 0600 hrs and 1800hrs

		if nightSound.Playing then nightSound:Stop() end
		wait(0.1)
		if not daySound.Playing and not CustomMusic.Playing then
			daySound:Play() 
			CustomMusic:Play()
			game.Lighting.FogStart = 0 
			game.Lighting.FogEnd = 100000 
			game.Lighting.FogColor = Color3.new(0,0,0)
		end
	end
end

Sure thing.

For starters, let’s simplify what you’re checking.
Like the monster script, you’re just checking what time it is, right?
Instead of using this:

	local Time = game.Lighting.TimeOfDay
	Time = (Time:gsub("[:\r]", ""))
	Time = tonumber(Time) -- Results in thousands

We can just use clock time again for simpler numbers.

local lighting = game:GetService("Lighting")
local Time = lighting.ClockTime -- Results in 0-24 numbers

Let’s make sure our if statements are simple, and clear. While you can check if a song is playing, it could be hard to read later!

Let’s use another flag like MonstersHaveSpawned, but we’ll make it a text string called CurrentTime for understanding. I’m going to set it to None for now.

local CurrentTime = "None" -- Can be Day/Night as well

We use CurrentTime so that the script will only send the Day/Night instructions once. It’s essentially just like asking “Hey, is it night already? If not, do this!”

if Time >= 6 and Time < 18 and CurrentTime ~= "Day" then -- it isnt day already?
		CurrentTime = "Day"
		
		nightSound:Stop()
		daySound:Play()
		
		lighting.FogStart = 0
		lighting.FogEnd = 100000
		daySound:Play()
		print("Day time")
		
	-- If it's more or equal then 18 at night
		-- And CurrentTime isnt set to night yet, then
	elseif Time >= 18 and CurrentTime ~= "Night"  then -- its not night already?
		CurrentTime = "Night"
		
		daySound:Stop()
		nightSound:Play()
		
		lighting.FogStart = 10
		lighting.FogEnd = 120
		lighting.FogColor = Color3.new(0,0,0)
		print("Night time")
	end

Combined with our update loop, this is the final script.

-- Get any services you need first to save time
local lighting = game:GetService("Lighting")

-- Use :WaitForChild() on the intro to scripts to prevent any missing errors.
local nightSound = workspace:WaitForChild("nightSound")
local daySound = workspace:WaitForChild("daySound")
local CustomMusic = workspace:WaitForChild("CustomMusic")

--[[ 
	We use a current time variable to prevent the excessive isPlaying checks.
	isPlaying is still a good way to check, but this seems simpler given the circumstances.
	It's a lot safer to assume things are playing if you told them to before.
--]]

local CurrentTime = "None" -- Feel free to set this to "Day" if you've already played the sounds


--[[ Avoid using wait() when possible!
	Wait() is more of a "there is a problem here" then it is for regular code use.
	I'd really only use wait() if you needed to time something specific or set how fast something should update
	(like this script)
--]]

while task.wait(0.3) do
	local Time = lighting.ClockTime
	
	-- Keep your coditional (if then) statements simple, it might hurt your head otherwise!
	
	-- If it's more or equal then 6 in the morning
	-- And it's earlier then 18 at night,
	-- And CurrentTime isnt set to Day yet, then
	if Time >= 6 and Time < 18 and CurrentTime ~= "Day" then
		CurrentTime = "Day"
		CustomMusic:Play()

		nightSound:Stop()
		daySound:Play()
		
		lighting.FogStart = 0
		lighting.FogEnd = 100000
		daySound:Play()
		print("Day time")
		
	-- If it's more or equal then 18 at night
		-- And CurrentTime isnt set to night yet, then
	elseif Time >= 18 and CurrentTime ~= "Night"  then
		CurrentTime = "Night"
		-- CustomMusic:Stop() -- im not sure if you wanted this or not
		daySound:Stop()
		nightSound:Play()
		
		lighting.FogStart = 10
		lighting.FogEnd = 120
		lighting.FogColor = Color3.new(0,0,0)
		print("Night time")
	end
	
end

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