I was thinking that too, but i don’t see how that could happen.
The tool is made of a handle who has a part Child who is the origin:
Here is the LocalScript who fetched the mouse input position:
local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local tool = plr.Backpack.Glock
tool.Equipped:Connect(function()
print("equipped gun")
uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print(input.Position)
script.Parent.Handler:FireServer(input.Position)
local mPos = input.Position
end
end)
end)
--gimzo visual debugger (not important)
uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mousepos = input.Position
local gun = script.Parent
local origin = gun:WaitForChild("Handle").Part.Position
local direction = (mousepos - origin).Unit * 10000
local rs = game:GetService("ReplicatedStorage")
local gizmo = require(rs.gizmo)
local RunService = game:GetService("RunService")
RunService:BindToRenderStep("DrawGizmos", 200, function()
gizmo.setColor("Bright red")
gizmo.drawRay(origin, direction)
gizmo.reset() -- resets the style
end)
end
end)
local re = script.Parent:WaitForChild("Handler")
re.OnServerEvent:Connect(function(plr, mPos)
local gun = script.Parent
local origin = gun.Handle.Part.Position
local studs = 300
local direction = (mPos - origin).Unit * studs --to be determined
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {gun.Handle, plr.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
print("the Vector3 value of the direction (x,y,z) "..direction.X.." "..direction.Y.." "..direction.Z)
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult then
print(raycastResult.Instance)
local bullet = Instance.new("Part")
bullet.CanCollide = false
bullet.Anchored = true
bullet.Position = raycastResult.Position
bullet.Parent = game.Workspace
if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
raycastResult.Instance.Parent.Humanoid:TakeDamage(40)
end
end
end)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mousepos = input.Position
local gun = script.Parent
local origin = gun:WaitForChild("Handle").Part.Position
local direction = CFrame.lookAt(origin, mousepos).LookVector*300
local rs = game:GetService("ReplicatedStorage")
local gizmo = require(rs.gizmo)
local RunService = game:GetService("RunService")
RunService:BindToRenderStep("DrawGizmos", 200, function()
gizmo.setColor("Bright red")
gizmo.drawRay(origin, direction)
gizmo.reset() -- resets the style
end)
end
end)
Is there a reason you’re using the Position property of the InputObject? If I’m not mistaken, I believe that’s the 2D position of the mouse, so at that point you’re trying to take 2D coordinates to get a 3D direction, which won’t work.
I believe Hit is what you want, which is a property that contains the position the mouse is pointing at in the game world.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local direction = (mouse.Hit.Position - origin).Unit --//New direction vector
Local Script:
local uis = game:GetService(“UserInputService”)
local plr = game.Players.LocalPlayer
local tool = plr.Backpack.Glock
tool.Equipped:Connect(function()
print("equipped gun")
uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print(input.Position)
script.Parent.Handler:FireServer(input.Position)
local mPos = input.Position
end
end)
end)
--gimzo visual debugger (not important)
uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mousepos = input.Position
local gun = script.Parent
local origin = gun:WaitForChild("Handle").Part.Position
local direction = CFrame.lookAt(origin, mousepos).LookVector*300
local rs = game:GetService("ReplicatedStorage")
local gizmo = require(rs.gizmo)
local RunService = game:GetService("RunService")
RunService:BindToRenderStep("DrawGizmos", 200, function()
gizmo.setColor("Bright red")
gizmo.drawRay(origin, direction)
gizmo.reset() -- resets the style
end)
end
end)
Server script:
local re = script.Parent:WaitForChild(“Handler”)
re.OnServerEvent:Connect(function(plr, mPos)
local gun = script.Parent
local origin = gun.Handle.Part.Position
local studs = 300
local direction = CFrame.lookAt(origin, mPos).LookVector*300 --to be determined
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {gun.Handle, plr.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
print("the Vector3 value of the direction (x,y,z) "..direction.X.." "..direction.Y.." "..direction.Z)
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult then
print(raycastResult.Instance)
local bullet = Instance.new("Part")
bullet.CanCollide = false
bullet.Anchored = true
bullet.Position = raycastResult.Position
bullet.Parent = game.Workspace
if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
raycastResult.Instance.Parent.Humanoid:TakeDamage(40)
end
end
end)
THANKS!
Result using the Mouse Object instead of the UserInputService!
I think Roblox should update their Mouse API Documentation and include that the Mouse Object is still better to use then the UserInputService in specific cases (since they say the UserInputService service is better then the mouse object) , especially for newer devs like me who wouldn’t know better!