Raycasting doesn't register parts sometimes

  1. What do you want to achieve? Keep it simple and clear!
    i want to make a gun for games im making(yes, more than 1 game no idea why but whatever)

  2. What is the issue? Include screenshots / videos if possible!
    even tho i inserted raycasting to scan for parts, it just ignores them half the time, this is what i mean: robloxapp-20220203-1806128.wmv (1.2 MB)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i tried slowing the bullet speed, trying to execute the wait function by RenderStepped from client(remoteevent in workspace since it was gonna be a singleplayer game(i know, probably wouldn’t be a great idea too idk why i tried that)), i tried setting the part’s cancollide on and other part’s properties and still nothing.

Note: this isn’t any error, it’s just raycasting not registering some parts for some reason, it prints no errors in output, but if i try to print the raycast result of the ray that didn’t register the block, it’s just nil

Raycast code where it prints nil sometimes:

	local dist = 0
	local params = RaycastParams.new()
	params.IgnoreWater = true
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = {workspace.Bullets,workspace.Blood,workspace.Terrain}
	
	while bullet do
		local pos,nextpos = bullet.Position,bullet.CFrame.LookVector*s.bulletSpd
		local r = workspace:Raycast(pos, bullet.CFrame.LookVector,params)
		if r and r.Instance and r.Instance.Transparency < 0.75 and not r.Instance:IsDescendantOf(t) and not r.Instance:IsDescendantOf(usr) then
			--prevent roblox script laziness, return if result is nil or result's instance is nil
			if not r or not r.Instance then return end
			local tr = bullet:FindFirstChildWhichIsA("Trail") or nil
			if tr then
				tr.Parent = workspace.Terrain
			end
			local at = Instance.new("Attachment",workspace.Terrain)
			at.WorldCFrame = getHitSurfaceCFrame(r.Position,r.Instance)
			bullet.CFrame = CFrame.new(r.Position)
			local bh = game.ServerStorage.BulletHole:Clone()
			bh.Position = r.Position
			bh.Orientation = at.WorldOrientation
			bh.Parent = workspace
			
			local human = r.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
			if human then
				human:TakeDamage(math.floor(math.random(s.dmg.min,s.dmg.max) ) )
				for _,v in pairs(game.ServerStorage.Particles._shoot.Blood:GetChildren()) do
					if v:IsA("ParticleEmitter") then
						local l = v:Clone()
						l.Parent = at
						l:Emit(l.Rate)
					end
				end
				
			else
				if r.Instance:FindFirstChild("Metal") then
					for _,v in pairs(game.ServerStorage.Particles._shoot.Metal:GetChildren()) do
						if v:IsA("ParticleEmitter") then
							local l = v:Clone()
							l.Parent = at
							l:Emit(l.Rate)
						end
					end
					local sounds = game.ServerStorage.Sounds.Gun.Metal:GetChildren()
					local cl = sounds[math.random(#sounds)]:Clone()
					cl.Parent = at
					cl:Play()
					bh.metal.Transparency = -1
					bh.Material = Enum.Material.Metal
				elseif r.Instance.Name == "_glass" then
					GlassShattering:Damage(r.Instance,r.Position,-bullet.CFrame.RightVector*5)
					for _,v in pairs(game.ServerStorage.Particles._shoot.Glass:GetChildren()) do
						if v:IsA("ParticleEmitter") then
							local l = v:Clone()
							l.Parent = at
							l:Emit(l.Rate)
						end
					end
					local sounds = game.ServerStorage.Sounds.Gun.Glass:GetChildren()
					local cl = sounds[math.random(#sounds)]:Clone()
					cl.Parent = at
					cl:Play()
					bh.glass.Transparency = 0
					bh.Material = Enum.Material.Glass
				else
					for _,v in pairs(game.ServerStorage.Particles._shoot.Ground:GetChildren()) do
						if v:IsA("ParticleEmitter") then
							local l = v:Clone()
							l.Parent = at
							l:Emit(l.Rate)
						end
					end
					local sounds = game.ServerStorage.Sounds.Gun.Ground:GetChildren()
					local cl = sounds[math.random(#sounds)]:Clone()
					cl.Parent = at
					cl:Play()
					bh.ground.Transparency = 0
					bh.Material = Enum.Material.Plastic
				end
			end
			local w = Instance.new("WeldConstraint",bh)
			w.Part0 = r.Instance
			w.Part1 = bh
			bh.Anchored = false
			
			game:GetService("Debris"):AddItem(at,10)
			game:GetService("Debris"):AddItem(bh,10)
			bullet:Destroy()
			break
		else
			bullet.Position = bullet.Position + nextpos
		end
		dist = dist + 1
		if dist >= 150 then
			bullet:Destroy()
			return
		end
		rs.Heartbeat:Wait()
	end

not sure what to try to fix anymore
(also if anyone is curious, yes i edited the GlassShattering module a bit but that’s not important so whatever)

1 Like

nevermind now it somehow works i have no idea what was that

1 Like