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)
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 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.
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
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)