Buggy Raycasting

  1. What do you want to achieve?
    A simple rain system that first, checks for an obstruction above the player (like a roof). If an obstruction is found, rain particles around the player are disabled. If no obstruction is found, then the particles are enabled. Here is my code.
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")

local Emitter = game:GetService("ReplicatedStorage").Emitter:Clone()

local RainParticle = Emitter.Rain.ParticleEmitter
local SplashParticle = Emitter.Splash.ParticleEmitter

local RainPart = RainParticle.Parent
local SplashPart = SplashParticle.Parent

local RainSound = game:GetService("SoundService").Rain
local IndoorSound = game:GetService("SoundService")["Indoor Rain"]

local RunService = game:GetService("RunService")

Emitter.Parent = game:GetService("Workspace") --Setting Default Values
RainParticle.Enabled = false
SplashParticle.Enabled = false

local Active = true

while wait(.25) do
	
	Humanoid.Died:Connect(function()
		Active = false
		Emitter:Destroy()
	end)
	
	if Active then	
		
		local Position = RootPart.Position - Vector3.new(0,2.75,0)
		Emitter:MoveTo(Position)
		
		if not RootPart then return end 
		
		local Origin = RootPart.Position
		local Direction = Origin + Vector3.new(0,100,0)
		local RayParameters = RaycastParams.new()
		RayParameters.FilterDescendantsInstances = {RainPart, SplashPart, Character:GetChildren()}
		RayParameters.FilterType = Enum.RaycastFilterType.Blacklist
		
		local RayResult = workspace:Raycast(Origin, Direction, RayParameters)
		
		if RayResult then --Obstruction
			if RayResult.Instance.ClassName == "MeshPart" then --MeshParts dont count as obstructions
				print("No Obstruction Located")
				RainParticle.Enabled = true
				SplashParticle.Enabled = true

				IndoorSound.Volume = 0
				IndoorSound.TimePosition = 0
				RainSound.Volume = 1
			else
				print("Obstruction Located")
				print(RayResult)
				RainParticle.Enabled = false
				SplashParticle.Enabled = false
						
				RainSound.Volume = .5
					--RainSound.TimePosition = 0
				IndoorSound.Volume = 1
				
			end
		elseif RayResult == nil then --No Obstruction
			print("No Obstruction Located")
			RainParticle.Enabled = true
			SplashParticle.Enabled = true
					
			IndoorSound.Volume = 0
			IndoorSound.TimePosition = 0
			RainSound.Volume = 1
		end
	end
end
  1. What is the issue?
    This system uses a simple raycast that fires straight up, but for some reason the raycast is being really buggy (detecting objects that arent above the player??? It fires straight up! I blacklisted the rain particle base and the character from being detected so it cant be that.)
  1. What solutions have you tried so far?
    I have no idea where to start on the problem but I thought it might have something to do with studio settings, so I tried it out but with no luck. Maybe there’s a problem with my code but I can’t figure it out. Everything seems to run fine in a separate studio tab. Any Ideas?

Heres a studio file to mess around with:
Dynamic Rain Testing.rbxl (40.3 KB)

Your script is checking whether the player is still alive and active in the game, if the player is alive the script will move the emitter to the player’s position and will cast a ray from the player’s position towards the sky, this ray is used to check for an obstruction above the player (like a roof).
It then checks the result of the raycast, if an obstruction is found, it will disable the rain and splash particles and play an indoor sound. If no obstruction is found, it will enable the rain and splash particles and play a rain sound.
Also, it checks if the obstruction is a meshpart and if it is, it won’t count it as an obstruction.
You can customize this script to match your needs, like adding more sound effects, or change the behavior of the rain.
It could also be optimized by not casting the ray every 0.25 seconds, but only when the player moves, or the player enters an area with a different roof.
Let me know if this helps or if you have any other questions.

Thank you for the reply and quick response time. I don’t know why but it looks like its working fine now. This has happened to me in the past, where scripts don’t work for some reason but when reloading studio they start working out of nowhere. Studio has me trippin. Again thanks!

1 Like

No problem! Lemme know if you need anymore help

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