How would I get a parts name from a ray?

Trying to make a basic gun.
(I have done some research on " FindPartOnRay" but have had no luck.)

Scripts:

--[LOCAL SCRIPT]--

local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("Remotes"):WaitForChild("Shot")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local debounce = false

script.Parent:WaitForChild("Equipt").Changed:Connect(function()
	mouse.Button1Down:Connect(function()
		if script.Parent:WaitForChild("Equipt").Value == true then
			if script.Parent.Reloading.Value == false then
				if script.Parent.Ammo.Value ~= 0 then
					if debounce == false then
					debounce = true
					
					local raycast = mouse.Hit.p
					print(raycast)
					wait()
					remote:FireServer(raycast)
					
					wait(0.5)
					debounce = false
					end
				end
			end
		end
	end)
end)



--[SERVER SCRIPT]--
local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("Remotes"):WaitForChild("Shot")

remote.OnServerEvent:Connect(function(player, raycast)
	local shotType = game.Workspace:FindFirstChild(player.Name):FindFirstChildOfClass("Tool").Name
	print(shotType)
	local partRaycast = replicatedStorage.raycast:Clone()
	local raycastPart = --parts name thats hit.
	print(raycastPart)
end)

Well, a fundamental issue is that you’re not even firing off a ray method in the first place, so you should fix that first. After that, it’s very easy to get the name of a part if you look at documentation first - the BasePart that the ray hits is passed as the first return value from the call, so you can just use Part.Name on it.

local Part = workspace:FindPartOnRay(args)
print(Part.Name)
1 Like
local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("Remotes"):WaitForChild("Shot")

remote.OnServerEvent:Connect(function(player, raycastPosition)
	local character = game.Workspace:FindFirstChild(player.Name)
	local shotType = game.Workspace:FindFirstChild(player.Name):FindFirstChildOfClass("Tool").Name
	print(shotType)
	local origin = game.Workspace:FindFirstChild(player.Name):FindFirstChildOfClass("Tool").Handle.Position
	local ray = Ray.new(origin, raycastPosition)
	local part = game.Workspace:FindPartOnRay(ray)
	print(part)
end)

Just prints handle?
local part = game.Workspace:FindPartOnRay(ray,player.Character)

The second parameter for :FindPartOnRay() is the Ignore Descendants List. This prevents the handle from being an obstruction.

Already have tried, just prints nil now…

	local ignore = game.Workspace:FindFirstChild(player.Name):FindFirstChildOfClass("Tool").Handle
	local ray = Ray.new(origin, raycastPosition)
	local part = game.Workspace:FindPartOnRay(ray, ignore)

Are you aiming at a part?
The ray might not be hitting anything, and that is why it prints nil.
Also, I recommend setting ignore to Player.Character, as you probably don’t want the player to shoot themselves.

1 Like

Heres a gif: https://im5.ezgif.com/tmp/ezgif-5-7e919cd2124a.gif

Mouse.Hit.p isnt a raycast its the mouse position and if u want to get the object you need to use Mouse.Hit which returns the object that the mouse is touching

2 Likes

Il look into it the post you have referenced.

1 Like

I don’t really see the issue here.
Since your issue is the raycasting part, maybe this article will help (Though the method they use for getting the mouse is deprecated, so just pay attention to the raycasting part)

1 Like

WeBuiltOurOwnWorld found the solution, thanks for your help though!

No it’s not. Mouse was never deprecated, and neither was having it as a parameter of Equipped.

1 Like