Mouse Target (AIM) system

Hi, I would like one of my attacks to depend on where the player’s mouse is but I have no idea how to do it.

I guess I’ll have to remove the predefined CFrame that I put on my shares but what will I have to replace it with ?

my local script :

local Player = game:GetService("Players").LocalPlayer
local mouse = Player:GetMouse()

local UIS = game:GetService("UserInputService")


local rp = game:GetService("ReplicatedStorage")
local rimuru = rp:WaitForChild("rimuru")

local debounce = false
local cooldown = 1

UIS.InputBegan:Connect(function(input,isTyping)
	if not isTyping then 
		if input.KeyCode == Enum.KeyCode.F then
			if debounce == false then
				debounce = true


				rimuru:FireServer()
			end 
		end
	end
end)

rimuru.OnClientEvent:Connect(function()
	wait(cooldown)
	debounce = false
end)

You can use mouse.Hit.p (a Vector3) and mouse.Hit (a CFrame) to work out where the player is looking.

1 Like

Its best to just create a new ray and have its Origin and end point rely on the mouse, since Mouse.TargetFilter can only filter one object

1 Like

could you explain to me what you mean by creating a new ray ?

local ray = Ray.new(mouse.UnitRay.Origin, Mouse.UnitRay.Direction)
1 Like

All this does is get the position of where the mouse would be if it were in a 3d plane for the origin and cast a ray

1 Like

ok I see UnitRay and the property to get the mouse origin if it is on a 3D plane.

don’t use deprecated functions like Ray.new()

2 Likes

Very true I just learned to use it that way so yeah. Raycast params allows you to change all the settings pretty easily, so its best to use it

1 Like

ok thank you for your help :+1: