Gun damages when the ray is in a certain angle

So basically my gun damages players if the ray is in a certain angle, and I don’t know why. Everthing prints, and no errors. Here is my script:

-- local script in gun (tool)
local BODY_DAMAGE = 20
local HEADSHOT = 40
local CLIP = 8
local MAX_BULLETS = 64
local SHOOTY_PART = script.Parent.Shooty
local HandleOfGun = script.Parent.Handle
local Part_1 = script.Parent.Part1
local Part_2 = script.Parent.Part2
local Part_3 = script.Parent.Part3
local Player = game.Players.LocalPlayer
local Character = Player.Character
local mouse = Player:GetMouse()
local Reload = false

script.Parent.Activated:Connect(function()
CLIP = CLIP - 1
if CLIP <= 0 then
   Reload = true
   CLIP = 8
else
   local MousePos = mouse.Hit.p
   game.ReplicatedStorage.Shooty:FireServer(Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART, MousePos, HandleOfGun, Part_1, Part_2, Part_3)
end
end)
-- Server script also in gun (tool)
local debris = game:GetService("Debris")
game.ReplicatedStorage.Shooty.OnServerEvent:Connect(function(player, Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART, MousePos, HandleOfGun, Part_1, Part_2, Part_3)
    local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Character, SHOOTY_PART, HandleOfGun, Part_1, Part_2, Part_3, workspace.Baseplate}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
    local ray = workspace:Raycast(SHOOTY_PART.Position, MousePos - SHOOTY_PART.Position.Unit * 100, raycastParams)
			local direction = (MousePos - SHOOTY_PART.Position).Unit * 100
			local midPoint = SHOOTY_PART.Position + direction/2
			local part = Instance.new("Part")
			part.Size = Vector3.new(0.2, 0.2,direction.Magnitude)
			part.CFrame = CFrame.new(midPoint, SHOOTY_PART.Position)
			part.Anchored = true
			part.CanCollide = false
			part.Parent = workspace
			part.BrickColor = BrickColor.new("New Yeller")
    		debris:AddItem(part,0.1)
if ray then
		local part = ray.Instance
		print(part)
		local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
		print("HumanoidFound")
		
		if humanoid then
			humanoid:TakeDamage(math.random(10,20))
		end
	end
end)

Help is appreciated and if you have questions, please ask me

I would really appreciate it if someone could help me oof :slight_smile:

Perhaps try using Ray.new() instead and see if that works. It might be a bug with the new Raycast API, as it is pretty new.

Can’t cause then it errors, 17:33:11.237 - Instance is not a valid member of Ray

You’re likely using Ray.new() wrong as it still definitely works, there’s no way it should error like that by design. Mind showing me how you implemented it?

local ray = workspace:Raycast(SHOOTY_PART.Position, MousePos - SHOOTY_PART.Position.Unit * 100, raycastParams)

to

local ray = Ray.new(SHOOTY_PART.Position, MousePos - SHOOTY_PART.Position.Unit * 100, raycastParams)

Is there something wrong?

The new raycast API is almost definitely not the problem. It’s the same code internally, just a different interface.

Can you describe your problem in more detail? What do you mean

?

Like literally it only damages player when the player who is shooting is close to the back of the head

You’re missing parentheses around your ray’s direction.

You have:

    local ray = workspace:Raycast(SHOOTY_PART.Position, MousePos - SHOOTY_PART.Position.Unit * 100, raycastParams)

It should be:

    local ray = workspace:Raycast(SHOOTY_PART.Position, (MousePos - SHOOTY_PART.Position).Unit * 100, raycastParams)

Also, why do you have a direction variable that you define after the ray? Why not do:

    local direction = (MousePos - SHOOTY_PART.Position).Unit * 100
    local ray = workspace:Raycast(SHOOTY_PART.Position, direction, raycastParams)
1 Like

Oh now its killing the player who has the gun, I tested it in a different game

Dunno. Just as a try (and a general improvement), don’t send the Character as an argument for your remote event. You can get the character from player.Character, no need to send it.