Hello, So i was making a script that would allow me to shoot at people after I press F. It’s been going great but the raycast is extremly off with the mouse. This is the line that calculates the mouse pos. This is a local script.
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local KillModuleEnabled = Player:WaitForChild("GameStats"):WaitForChild("KillModuleEnabled")
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(Input, processed)
if Input.KeyCode == Enum.KeyCode.F then
KillModuleEnabled.Value = not KillModuleEnabled.Value
if KillModuleEnabled.Value == true and Player.UserId == 182343133 then
Mouse.Button1Down:Connect(function()
game.ReplicatedStorage.RemoteEvents.ShootFromPlayerEvent:FireServer(Mouse.Hit.LookVector, KillModuleEnabled.Value)
end)
end
end
end)
Hi!
I believe what you are looking for is mouse.Hit as a CFrame itself. Since mouse is a legacy object, and you are raycasting “on demand”, I suggest you use the following approach instead.
Find mouse position on the screen (in pixels) using UserInputService:GetMouseLocation().
Use gathered coordinates in camera:ScreenPointToRay() function, which provides a UnitRay as a return.
Extend the UnitRay and check for results.
Example standalone code:
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local RAY_LENGTH = 800
local player = game:GetService("Players").LocalPlayer
local camera = workspace.CurrentCamera
local function Shoot(actionName,inputState,inputObject)
if inputState ~= Enum.UserInputState.Begin then return end
local mousePos = UIS:GetMouseLocation()
local unitRay = camera:ScreenPointToRay(mousePos.X,mousePos.Y)
local raycastResult = workspace:Raycast(unitRay.Origin,unitRay.Direction*RAY_LENGTH)
if raycastResult then
local part = Instance.new("Part")
part.BrickColor = BrickColor.new("Really red")
part.Anchored = true; part.CanCollide = false
part.Position = raycastResult.Position
part.Parent = workspace
end
end
CAS:BindAction("Shoot",Shoot,false,Enum.KeyCode.F)
note: ray origin may also be some other position, such as Head or HumanoidRootPart.
EDIT. @kinglol123468 I wouldn’t have sent the script if it didn’t work.
It is bound to F key.
Although I suggest the approach above, I’m also pasting the version involving mouse object. Mouse already provides you with necessary information, so raycasting isn’t necessary. It has mouse.Hit, mouse.Target, mouse.TargetFilter and other properties.
local CAS = game:GetService("ContextActionService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local function Shoot(actionName,inputState,inputObject)
if inputState ~= Enum.UserInputState.Begin then return end
print(mouse.Target)
if mouse.Target then
local part = Instance.new("Part")
part.BrickColor = BrickColor.new("Really red")
part.Anchored = true; part.CanCollide = false
part.Position = mouse.Hit.Position
part.Parent = workspace
end
end
CAS:BindAction("Shoot",Shoot,false,Enum.KeyCode.F)
Ok it works when I press F. So thank you for that. I’ll try to incorporate this with my shooting system. But one more questions. How can I make it detect if the player clicks?
No problem!
In case you would like to listen for specific mouse keys, you would compare UserInputType instead of KeyCode. The following should work. Good luck!
UserInputService:
UIS.InputBegan:Connect(function(input,gameProcessed)
if gameProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("Clicked")
end
end)
ContextActionService:
local function LeftMouseClick(actionName,inputState,inputObject)
if inputState == Enum.UserInputState.Begin then
print("Clicked")
end
end
CAS:BindAction("LeftMouseClick",LeftMouseClick,false,Enum.UserInputType.MouseButton1)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Button1Down:Connect(function()
local mousePos = mouse.Hit --cframe value of current mouse position in window
local mousePos2 = mouse.Hit.p --vector3 value of current mouse position in window
end)
Getting the mouse position from the “mouse” instance after a left mouse button click is relatively easy, you just need to make use of the “Button1Down” event which fires whenever the left mouse button is clicked.
just a note, its not good to check on the client, since hackers can just easily bypass it. Instead, check it on the server where you listen for the game.ReplicatedStorage.RemoteEvents.ShootFromPlayerEvent