Lightning script is not working

hello!

im trying to make a Lightning script, the script will choose a random object form the workspace and when the script chose it i want the Lightning to spawn above that chosen object but the problem is the Lightning keep spawning and the same object how do i fix that?

the script:

local ligghting = script:WaitForChild("Configuration").Rain.LightningMain:Clone()
local lighPosiIdle = Vector3.new(0,0,0)
ligghting.Position = lighPosiIdle



repeat
	wait(0.01)
	local randomthunder = math.random(1,100)
	--print("ThenumIs..."..randomthunder)
until randomthunder == 55

local lighting = true

if ligghting then
	ligghting.Parent = game.Workspace
	
		local randomobj = game.Workspace:GetDescendants()
	local randob = randomobj[math.random(1, #randomobj)]
	if randob:IsA("Model") then
		if randob.Parent:FindFirstChild("Part") then
			print(randob.Name)
			ligghting.Position = randob.Parent:FindFirstChild("Part").Position+Vector3.new(0,20,0)
			
		elseif randob.Parent:FindFirstChild("HumanoidRootPart") then
			ligghting.Position = randob.Parent:FindFirstChild("HumanoidRootPart").Position+Vector3.new(0,20,0)
		end
		
	else
		if randob:IsA("Part") then
			ligghting.Position = randob.Position+Vector3.new(0,20,0)
		end
	end
end

end

Are there any errors, and do you only want this thunder to appear once?
Is it a Script or LocalScript?

You are using the variable names lighting and ligghting.

1 Like
local config = script:WaitForChild("Configuration")
local rain = config:WaitForChild("Rain")
local lightning = rain:WaitForChild("LightningMain")
local lightningClone = lightning:Clone()
local lighPosiIdle = Vector3.new(0,0,0)
lightningClone.Position = lighPosiIdle

while task.wait() do
	repeat
		task.wait()
		local randomthunder = math.random(1, 100)
	until randomthunder == 55

	if lightningClone then
		lightningClone.Parent = workspace

		local randomobj = workspace:GetDescendants()
		local randob = randomobj[math.random(1, #randomobj)]
		if randob:IsA("BasePart") then
			lightningClone.Position = randob.Position+Vector3.new(0,20,0)
		end
	end
end

I just added waits on required assets/instances and got rid of some redundant code and put everything inside a while task.wait() do loop so that it runs indefinitely. If you want lightning to strike more frequently you can reduce the math.random() range so that the RNG isn’t a 1 in 100 chance per frame.

1 Like