Ray not shooting in correct direction

image
the ray is supposed to shoot the noob npc but it goes in a completely different direction

local db = false

local settings = {
	["Damage"] = 20;
	["Bullet_Color"] = "New Yeller";
	["Bullet_Range"] = 100; -- (In studs)
	["Time_Before_Bullet_Disappears"] = .5;
	["Cast_Ray"] = true;
	["CoolDown"] = 1;
}



function Shoot(TG)
	if not db then
		db = true
		
		local humanoid = TG:WaitForChild("Humanoid")
		
		local collideRay = Ray.new(handle.CFrame.p, (handle.CFrame.p - TG.PrimaryPart.CFrame.p).unit * settings["Bullet_Range"])
		local collidePart, positionPart = workspace:FindPartOnRay(collideRay, script.Parent, false, true)

		local ignore = {script.Parent}

		if collidePart then
			if collidePart.CanCollide == false and collidePart.Parent then
				if not collidePart.Parent:FindFirstChild("Humanoid") then
					table.insert(ignore, collidePart)
				end
			end		
		end

		local ray = Ray.new(handle.CFrame.p, (handle.Muzzel.WorldCFrame.p - TG.PrimaryPart.CFrame.p).unit * settings["Bullet_Range"])
		local part, position = workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)

		if settings["Cast_Ray"] == true then
			local bullet = Instance.new("Part",workspace)
			bullet.BrickColor = BrickColor.new(settings["Bullet_Color"])
			bullet.CanCollide = false
			bullet.Name = "Bullet"
			bullet.FormFactor = "Custom"
			bullet.Anchored = true
			bullet.BottomSurface = "Smooth"
			bullet.TopSurface = "Smooth"

			local distance = (handle.CFrame.p - position).magnitude
			bullet.Size = Vector3.new(0.2, 0.2, distance)
			bullet.CFrame = CFrame.new(handle.Muzzel.WorldCFrame.p, position) * CFrame.new(0, 0, -distance / 2)
			game:GetService("Debris"):AddItem(bullet, settings["Time_Before_Bullet_Disappears"])
		end

		if part ~= nil then				
			if part.Parent:FindFirstChild("Humanoid") and part.Parent.Parent == workspace.Allies or game.Players:GetPlayerFromCharacter(part.Parent) then
				part.Parent.Humanoid:TakeDamage(settings["Damage"])
			end
		end
		task.wait(settings["CoolDown"])
		db = false
	end
end

TG is the noob npc and handle is the gun that the other guy is holding and muzzel is an attachment inside the handle

Try flipping this around, and also make sure it’s using the exact same position as the origin point

(TG.PrimaryPart.Position - handle.CFrame.Position).Unit * settings["Bullet_Range"]

Also, if handle is an Attachment, you have to use .WorldCFrame instead since regular .CFrame for Attachments is the offset relative to the part they’re parented to


Also I can’t help but notice some pretty alarming issues with the rest of your code:

This is deprecated. It might break in the future because Roblox no longer supports it

So is this, use workspace:Raycast instead for both of these

Do not instantiate with the parent argument if you care about performance. Reason here. Only assign the parent after you’ve set all the properties

1 Like

now its just backwards but still at the wrong angle