Rain System Working

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local HRP = char:FindFirstChild('HumanoidRootPart')
		local Humanoid = char:FindFirstChild('Humanoid')
		
		local RainSpawner = Instance.new('Part', workspace)
		RainSpawner.Name = 'RainSpawner'
		RainSpawner.Anchored = true
		RainSpawner.CanCollide = false
		RainSpawner.Size = Vector3.new(60, 1, 60)
		RainSpawner.Position = Vector3.new(HRP.Position.X , HRP.Position.Y + 34, HRP.Position.Z)
		
		local Rain = coroutine.create(function()
			while true do
				task.wait()
				local rainPart = Instance.new('Part', workspace)
				rainPart.Name = 'Rain'
				rainPart.Color = Color3.new(0, 0, 1)
				rainPart.Size = Vector3.new(0.25, 0.25, 0.25)
				rainPart.CanCollide = false

				local x = RainSpawner.Position.X * 0.5
				local y = RainSpawner.Position.Y * 0.5
				local z = RainSpawner.Position.Z * 0.5

				rainPart.Position = RainSpawner.Position + Vector3.new(math.random(0 - x, x), math.random(0 - y, y), math.random(0 - z, z))
				game.Debris:AddItem(rainPart, 5)
			end
		end)
		
		coroutine.resume(Rain)
		
		while true do
			task.wait()
			if Humanoid.Health == 0 then
				RainSpawner:Destroy()
			end
			RainSpawner.Position = Vector3.new(HRP.Position.X , HRP.Position.Y + 34, HRP.Position.Z)
		end
	end)
end)

So essentially my code is basically creating an area above the player from which the rain parts are going to fall from. The system already worked and was running without an issue, however, when I added the couroutine to actually make the rain parts fall I receive an error.
image
I would like to note that sometimes if I don’t make when I load I don’t immediately receive this error, but the code still doesn’t work in the way intended

Can y’all tell me why this bug is happening? I can try to fix it myself if y’all tell me the issue, but if i cant then slide the solution.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local HRP = char:FindFirstChild('HumanoidRootPart')
		local Humanoid = char:FindFirstChild('Humanoid')
		
		local RainSpawner = Instance.new('Part', workspace)
		RainSpawner.Name = 'RainSpawner'
		RainSpawner.Anchored = true
		RainSpawner.CanCollide = false
		RainSpawner.Size = Vector3.new(60, 1, 60)
		RainSpawner.Position = Vector3.new(HRP.Position.X , HRP.Position.Y + 34, HRP.Position.Z)
		
		local Rain = coroutine.create(function()
			while true do
				task.wait()
				local rainPart = Instance.new('Part', workspace)
				rainPart.Name = 'Rain'
				rainPart.Color = Color3.new(0, 0, 1)
				rainPart.Size = Vector3.new(0.25, 0.25, 0.25)
				rainPart.CanCollide = false

				local x = RainSpawner.Size.X * 0.5
				local y = RainSpawner.Size.Y * 0.5
				local z = RainSpawner.Size.Z * 0.5
				
				rainPart.Position = RainSpawner.Position + Vector3.new(math.random(0 - x, x), 0, math.random(0 - z, z))
				game.Debris:AddItem(rainPart, 5)
			end
		end)
		
		coroutine.resume(Rain)
		
		while true do
			task.wait()
			if Humanoid.Health == 0 then
				RainSpawner:Destroy()
			end
			RainSpawner.Position = Vector3.new(HRP.Position.X , HRP.Position.Y + 34, HRP.Position.Z)
		end
	end)
end)

NVM, I was accidentally using the RainSpawner Position instead of size. It works now.

2 Likes

It could be because X, Y or Z are zero. When you do math.random(0 - X, X), then if X is negative, it will make it math.random(+Z, -Z). That is likely what is producing your error.

As in, because the coordinate is positive, subtracting it causes it to become positive. Your start for math.random is then positive, while the end of the interval is negative, which causes this issue.

1 Like

It was because of this, however the reason the second argument was being negative was because I was using the RainSpawner’s Position Instead of Size which was my fault.

1 Like

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