Power outage script

in a game I made, I have access to a GUI that lets me do specific things, one of which is a power outage that brings all the lights into replicated storage and undo when clicked on again.

however, the problem is the effect i’m trying to achieve: i would like the lights to break out one by one in a random interval between 0.5 seconds to 1 second. everytime i’ve tried to achieve this effect, It only uses one number, which is for some reason always either 0 or 1. this is my current script:

local Event = game.ReplicatedStorage.PowerEvent
local On = game.ReplicatedStorage.OnEvent
local light = workspace.lightbreak
random = math.random(0.7, 1.1)


Event.OnServerEvent:Connect(function(Player, Transparency)
	game.Workspace.Rain:Stop()
	game.Workspace.Sound:Pause()
	for i,v in pairs(game.Workspace.Lights:GetChildren()) do
		v.Parent = game.ReplicatedStorage
		light.PlayOnRemove = true
		local mom = light.Parent
		local generatedsound = light:Clone()
		generatedsound.Parent = mom
		generatedsound:Destroy()
		print(random)
		task.wait(random)
	end	
	task.wait(2)
	game.Workspace.Ambiance:Play()
end)

On.OnServerEvent:Connect(function(Player, Transparency)
	game.Workspace.Ambiance:Stop()
	for key, value in pairs(game.ReplicatedStorage:GetDescendants()) do
        if value.Name == "Light" then
			local clone = value:Clone()
			clone.Parent = workspace.Lights 
			value:Destroy() 
			game.Workspace.Sound:Resume()
			game.Workspace.Rain:Play()
		end
	end
end)


hopefully my explanation is clear enough

3 Likes

This is due to you seting the variable “random” to a number from 0.7 to 1.1 just once

You dont need to set a variable for this and can just do task.wait(math.random(.7, 1.1))

Cleaned up your code a bit; hope you dont mind

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local light = workspace.lightbreak

local rain_sound = workspace.Rain
local sound = workspace.Sound

ReplicatedStorage.PowerEvent.OnServerEvent:Connect(function(Player)
	rain_sound:Stop()
	sound:Pause()
	
	for _,v in workspace.Lights:GetChildren() do
		v.Parent = ReplicatedStorage
		light.PlayOnRemove = true
		
		local generatedsound = light:Clone()
		generatedsound.Parent = light.Parent
		generatedsound:Destroy()
		
		task.wait(math.random(7, 11)/10)
	end	
	task.wait(2)
	game.Workspace.Ambiance:Play()
end)

ReplicatedStorage.OnEvent.OnServerEvent:Connect(function(Player) -- also what is this meant to do?
	workspace.Ambiance:Stop()
	for _, value in ReplicatedStorage:GetDescendants() do
		if value.Name == "Light" then
			local clone = value:Clone()
			clone.Parent = workspace.Lights 
			value:Destroy()
			sound:Resume()
			rain_sound:Play()
		end
	end
end)
4 Likes

To add to what Hzodx said:

math.random() only works with whole numbers.

If you wanted to do a random number from 0.7 to 1.1, you could do math.random(7, 11)/100.
Basically just shift the decimal place over until there is none, then compensate for that by dividing the random result. Giving it decimal numbers just causes them to be rounded down (which gives you the 0 and 1 result).

2 Likes

Adding to what matthewleg0 says

math.random() – (no inputs)
returns a random number between 0 and 1 (with decimals)

4 Likes

will keep this in mind from now on; i did not know that was how it worked. thanks alot!

2 Likes

thanks! i only set it as a variable so i could print and figure out what numbers are coming out of the math.random() function, that’s my bad. thanks for cleaning up the code also!

2 Likes

also - the other part of the script is so that i can undo said power outage by right clicking on the button

1 Like

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