✓ Accepted answer
So this script basically gets the position from the camera to the point in world space and then makes a new ray from your gun barrel to where ever the click location is at. I have explained a bit more with comments.
local tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local equipped = false
local character = player.Character:GetChildren()
local toolOrigin = character:FindFirstChildOfClass("Tool").tool.barrel.Position
local RE = game.ReplicatedStorage:WaitForChild("RemoteEvent1")
tool.Equipped:Connect(function()
equipped = true
end)
tool.Unequipped:Connect(function()
equipped = false
end)
function getMouseHit(x,y)
local ray = Camera:ScreenPointToRay(x, y, 0)
local ray1 = Ray.new(ray.Origin,ray.Direction * 1000)
local part, hitPos, norm = workspace:FindPartOnRayWithIgnoreList(ray1,character)
return CFrame.new(hitPos,ray1.Origin)*CFrame.Angles(0,math.rad(180),0)
end
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if equipped and not gameProcessed and input.UserInputType == Enum.UserInputType.MouseButton1 then
local worldCF = getMouseHit(input.Position.X,input.Position.Y)
RE:FireServer(toolOrigin,worldCF,character)
end
end)
Server script:
local RE = Instance.new("RemoteEvent")
RE.Name = "RemoteEvent1"
RE.Parent = game.ReplicatedStorage
RE.OnServerEvent:Connect(function(player,toolOrigin,worldCF,character)
local ray = Ray.new(toolOrigin,(worldCF.Position-toolOrigin).Unit)
local ray1 = Ray.new(ray.Origin,ray.Direction * 1000)
local part, hitPos, norm = workspace:FindPartOnRayWithIgnoreList(ray1,character)
if part then
print(part," yay!")
else
print(part," awww")
end
end)
3 comments
the pos is mouse.Hit.p
ok Im making you a whole new script
ok