Can someone help me loop this script?

Hello so i found a Rain Local script but i can’t loop it i tried to do while true do then wait but it didnt worked if someone can help its

local plr = game.Players.LocalPlayer
local char = plr.Character
local head = char.Head

local rainpart = script.RainPart

local rainRay = Ray.new(head.Position,Vector3.new(0,1000,0)) – Goes up from player’s head.

local ignoreTable = {char, rainpart}

local rs = game:GetService(“RunService”)

script.Sound:Play()
script.Sound2:Play()

rs.RenderStepped:Connect(function()

local plr = game.Players.LocalPlayer
local char = plr.Character
local head = char.Head

local rainRay = Ray.new(workspace.CurrentCamera.CFrame.Position,Vector3.new(0,1000,0)) -- Goes up from player's head.

for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("BasePart") then
		if v.Transparency >= 1 then
			table.insert(ignoreTable,i+2,v)
			wait()
		end
	end
end

local hit, position = workspace:FindPartOnRayWithIgnoreList(rainRay, ignoreTable)




if hit then
	rainpart.Parent = script
	
	
	if script.Sound2.Volume == 0 then
		local part = script.Sound
		local tween = game:GetService("TweenService")
		
		local tweeningInfo = TweenInfo.new(
			
			1.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPor = {
			
			Volume = 0
		     --Position = Vector3.new(0, 10, 10)
		     --SetPrimaryPartCFrame(workspace.pepos)
		}
		local RTween = tween:Create(part, tweeningInfo, partPor)
		RTween:Play()
		
		-- VOLUME UP
		
		
		local part = script.Sound2
		local tween = game:GetService("TweenService")
		
		local tweeningInfo = TweenInfo.new(
			
			1.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPor = {
			
			Volume = 0.5
		     --Position = Vector3.new(0, 10, 10)
		     --SetPrimaryPartCFrame(workspace.pepos)
		}
		local RTween = tween:Create(part, tweeningInfo, partPor)
		RTween:Play()
	end
				
			
	
	print("Ray was hit.")
else
	
	if script.Sound.Volume == 0 then
		local part = script.Sound
		local tween = game:GetService("TweenService")
		
		local tweeningInfo = TweenInfo.new(
			
			1.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPor = {
			
			Volume = 3
		     --Position = Vector3.new(0, 10, 10)
		     --SetPrimaryPartCFrame(workspace.pepos)
		}
		local RTween = tween:Create(part, tweeningInfo, partPor)
		RTween:Play()
		
		
		
		-- VOLUME DOWN
		
		
		
		local part = script.Sound2
		local tween = game:GetService("TweenService")
		
		local tweeningInfo = TweenInfo.new(
			
			1.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPor = {
			
			Volume = 0
		     --Position = Vector3.new(0, 10, 10)
		     --SetPrimaryPartCFrame(workspace.pepos)
		}
		local RTween = tween:Create(part, tweeningInfo, partPor)
		RTween:Play()
	end
	
	
	
	rainpart.Parent = workspace
	rainpart.Position = Vector3.new(workspace.CurrentCamera.CFrame.Position.X, workspace.CurrentCamera.CFrame.Position.Y+10, workspace.CurrentCamera.CFrame.Position.Z)
	print("No part found.")
end

end)

Hi! You probably won’t get a lot of help on this post without putting in more effort to your question.

Can you please edit your post to follow the guidelines for Scripting Support:

  • Properly format your code by putting three backticks ``` at the top and bottom of the code part.
  • Explain:
    • what the code you found currently does and how it works,
    • what you want it to do in addition,
    • and what exactly you already tried to accomplish that.
1 Like

its a rain local script its in starter gui it make rain whit particle emmiter they are two part inside the script whit particle emmiter i want it to like every 300 sec it make rain then stop a loop i tried to looped it but it didnt work so

You can use a while true loop to loop through your code

while wait(300) do
    --Code you want to loop
end

Okay i gonna try and see if it works thanks

Hey its not stopping it keep raining :stuck_out_tongue:

You cannot just throw a while loop around this code, it will not work.

There are much better ways to do this, but the easiest is to just check on every frame if it should be raining or not.

Do this:

  1. At the top of the script, put local isRaining = false
  2. After line 16 (rs.RenderStepped:Connect...), put if isRaining then
  3. Before the final line end), put a matching end.
  4. At the very bottom of the script, put the code to toggle isRaining:
    while true do
        wait(10)
        isRaining = true
        wait(10)
        isRaining = false
    end
    

As a side note: the script you found is pretty poorly written in the first place. A proper solution would probably involve totally rewriting it :slight_smile:

Wow if this work i will really thank u bro

i did what u said and it still dont work there is no error in the output

''while true do
wait(2)
isRaining = true
wait(2)
isRaining = false
end

local isRaining = false
local plr = game.Players.LocalPlayer
local char = plr.Character
local head = char.Head

local rainpart = script.RainPart

local rainRay = Ray.new(head.Position,Vector3.new(0,1000,0)) – Goes up from player’s head.

local ignoreTable = {char, rainpart}

local rs = game:GetService(“RunService”)

script.Sound:Play()
script.Sound2:Play()

rs.RenderStepped:Connect(function()
if isRaining then
local plr = game.Players.LocalPlayer
local char = plr.Character
local head = char.Head

local rainRay = Ray.new(workspace.CurrentCamera.CFrame.Position,Vector3.new(0,1000,0)) -- Goes up from player's head.

for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("BasePart") then
		if v.Transparency >= 1 then
			table.insert(ignoreTable,i+2,v)
			wait()
		end
	end
end

local hit, position = workspace:FindPartOnRayWithIgnoreList(rainRay, ignoreTable)




if hit then
	rainpart.Parent = script
	
	
	if script.Sound2.Volume == 0 then
		local part = script.Sound
		local tween = game:GetService("TweenService")
		
		local tweeningInfo = TweenInfo.new(
			
			1.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPor = {
			
			Volume = 0
		     --Position = Vector3.new(0, 10, 10)
		     --SetPrimaryPartCFrame(workspace.pepos)
		}
		local RTween = tween:Create(part, tweeningInfo, partPor)
		RTween:Play()
		
		-- VOLUME UP
		
		
		local part = script.Sound2
		local tween = game:GetService("TweenService")
		
		local tweeningInfo = TweenInfo.new(
			
			1.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPor = {
			
			Volume = 0.5
		     --Position = Vector3.new(0, 10, 10)
		     --SetPrimaryPartCFrame(workspace.pepos)
		}
		local RTween = tween:Create(part, tweeningInfo, partPor)
		RTween:Play()
	end
				
			
	
	print("Ray was hit.")
else
	
	if script.Sound.Volume == 0 then
		local part = script.Sound
		local tween = game:GetService("TweenService")
		
		local tweeningInfo = TweenInfo.new(
			
			1.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPor = {
			
			Volume = 3
		     --Position = Vector3.new(0, 10, 10)
		     --SetPrimaryPartCFrame(workspace.pepos)
		}
		local RTween = tween:Create(part, tweeningInfo, partPor)
		RTween:Play()
		
		
		
		-- VOLUME DOWN
		
		
		
		local part = script.Sound2
		local tween = game:GetService("TweenService")
		
		local tweeningInfo = TweenInfo.new(
			
			1.5,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local partPor = {
			
			Volume = 0
		     --Position = Vector3.new(0, 10, 10)
		     --SetPrimaryPartCFrame(workspace.pepos)
		}
		local RTween = tween:Create(part, tweeningInfo, partPor)
		RTween:Play()
	end
	
	
	
	rainpart.Parent = workspace
	rainpart.Position = Vector3.new(workspace.CurrentCamera.CFrame.Position.X, workspace.CurrentCamera.CFrame.Position.Y+10, workspace.CurrentCamera.CFrame.Position.Z)
	print("No part found.")
	end
	end

end)‘’

You put it at the top, so the script is looping forever at the top.

Yes, everything else is fine. Just move the while loop to the bottom.

can i add u on discord mike so i could send u the script

still not working i did all what u did :confused:

I don’t think my suggestions are the problem.

The whole script you posted is terrible. Where did you find it?

not gonna lie in the toolbox :stuck_out_tongue: but i really need a rain script im making a big city game i am a builder and not a scripter so

nicemike40 can i add you on roblox i want to show u something