Trying raycastParams, but can't detect parts

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punch = Instance.new("RemoteEvent", ReplicatedStorage)
punch.Name = "HamonAK"
local fireSound = script.fire

local function GunTrail(startPos, endPos)
	local trail = Instance.new("Part", game.Workspace)
	trail.Anchored = true
	trail.CanCollide = false
	trail.Material = "Neon"
	trail.BrickColor = BrickColor.new("Neon orange")
	trail.Size = Vector3.new(0.2, 0.2, (startPos - endPos).magnitude)
	trail.Position = startPos
	trail.CFrame = CFrame.new(startPos, endPos)
	trail.CFrame = trail.CFrame + trail.CFrame.LookVector * (trail.Size.Z / 2)
	spawn(function()
		wait()
		trail:Destroy()
	end)
end

local function hitcheck(player, mouse)
	if player.pushct.Value == false and player.holdsword.Value == false and player.playertype.Value == "Soviet hamon" then
		player.pushct.Value = true
		spawn(function()
			wait(10)
			player.pushct.Value = false
		end)
		local remote
		local character = player.Character or player.CharacterAdded:Wait()
		local weapon = game.ServerStorage.ak47:Clone()
		weapon.CFrame = character.RightHand.CFrame + character.RightHand.CFrame.UpVector * -0.5 + character.RightHand.CFrame.LookVector * 0.15
		local angleX, angleY, angleZ = weapon.CFrame:ToEulerAnglesXYZ()
		weapon.CFrame = weapon.CFrame * CFrame.Angles(math.rad(angleX - 90), 0, 0)
		weapon.Parent = character.RightHand
		local connecter = Instance.new("WeldConstraint", character.RightHand)
		connecter.Part0 = character.RightHand
		connecter.Part1 = weapon
		player.hitable.Value = true
		wait(1)
		for count = 1, 30 do
			if weapon:FindFirstChild("fire") then
				weapon.fire:Play()
			else
				local copy = fireSound:Clone()
				copy.Parent = weapon
				copy:Play()
			end
			print("boom")
			local ray = RaycastParams.new()
			ray.FilterType = Enum.RaycastFilterType.Whitelist
			ray.FilterDescendantsInstances = {weapon, character}
			local detects = workspace:Raycast((weapon.CFrame + weapon.CFrame.LookVector * 1).p, weapon.CFrame.LookVector * 100, ray)
			if detects then
				local endposition = detects.Position
				print(endposition)
				GunTrail((weapon.CFrame + weapon.CFrame.LookVector * 1).p, endposition)
				if player.pvpmode.Value == false then
					if detects.Parent.Parent == game.Workspace.clones and detects.Parent:FindFirstChild("Humanoid") then
						local blood = game.ServerStorage.blood:Clone()
						blood.Parent = game.Workspace
						blood.Position = detects.Position
						detects.Parent.Humanoid.Health = detects.Parent.Humanoid.Health - 5
					end
				else
					if detects.Parent.Parent == game.Workspace then
						for _, v in pairs(game.Players:GetChildren()) do
							if v.Name == detects.Parent.Name then
								if v.pvpmode.Value == true then
									player.hitable.Value = false
									local blood = game.ServerStorage.blood:Clone()
									blood.Parent = game.Workspace
									blood.Position = detects.Position
									if detects.Parent.Humanoid.Health <= 5 then
										detects.Parent.Humanoid.Health = 0.01
									else
										detects.Parent.Humanoid.Health = detects.Parent.Humanoid.Health - 5
									end
								end
							end
						end
					end
				end
			else
				print("nothing found")
				GunTrail((weapon.CFrame + weapon.CFrame.LookVector * 1).p, (weapon.CFrame + weapon.CFrame.LookVector * 100).p)
			end
			wait(0.1)
		end
		connecter:Destroy()
		weapon:Destroy()
		player.hitable.Value = false
		print(player.hitable.Value)
	end
end

punch.OnServerEvent:Connect(hitcheck)

I tried to make ray casting gun and It says nothing is in ray even there’s part.
what’s problem?

2 Likes

Heya! o/

ray.FilterType = Enum.RaycastFilterType.Whitelist
ray.FilterDescendantsInstances = {weapon, character}

You are setting a Whitelist. So only parts inside that list will be considered for the raycast hit.
And you are adding to that list the weapon and the character.
If you are planning to hit only the weapon and the character, then everything is alright. But if you want to ignore the weapon and character and read everything else in the game with the ray, then change that whitelist to Blacklist.

Idk if theres something else in the script. I should read it carefully to know, but thats the first thing I noticed.