Mouse position inaccurate

Hello, I’m having some issues with inaccurate mouse position. this is my current code:

local MousePos = UIS:GetMouseLocation()
local Ray_ = workspace.CurrentCamera:ViewportPointToRay(MousePos.X, MousePos.Y)
local RayResult = workspace:Raycast(Ray_.Origin, Ray_.Direction * 2000)
		
if RayResult then
	script.Shoot:FireServer(Tool.Name, {X = RayResult.Position.X; Z  = RayResult.Position.Z; Y = RayResult.Position.Y})
end

Is there a way to fix it?

3 Likes

Is it really inaccurate or just a bit? I think there is a 36 pixel offset for the roblox topbar so you can try substracting it to MousePos.Y

yeah just a bit so, UDim2.new(MousePos.X.Scale, MousePos.X.Offset, MousePos.Y.Scale,Mouse.Pos.Y.Offset -36)?

local MousePos = UIS:GetMouseLocation()
local screenSize = UIS:GetScreenSize()

local MousePosViewport = Vector2.new(MousePos.X / screenSize.X, MousePos.Y / screenSize.Y)

local Ray = workspace.CurrentCamera:ViewportPointToRay(MousePosViewport.X, MousePosViewport.Y)

local RayResult = workspace:Raycast(Ray.Origin, Ray.Direction * 2000)

if RayResult then
	script.Shoot:FireServer(Tool.Name, {X = RayResult.Position.X; Z  = RayResult.Position.Z; Y = RayResult.Position.Y})
end

Try this, tell me if worked

unfortunately GetScreenSize() doesn’t exist

Oh sheesh try this, tell me now.

local MousePos = UIS:GetMouseLocation()
local screenSize = workspace.CurrentCamera.ViewportSize

local MousePosViewport = Vector2.new(MousePos.X / screenSize.X, MousePos.Y / screenSize.Y)

local Ray = workspace.CurrentCamera:ViewportPointToRay(MousePosViewport.X, MousePosViewport.Y)

local RayResult = workspace:Raycast(Ray.Origin, Ray.Direction * 2000)

if RayResult then
	script.Shoot:FireServer(Tool.Name, {X = RayResult.Position.X; Z  = RayResult.Position.Z; Y = RayResult.Position.Y})
end

nop, seems like RayResult is nil (no errors tho)

local ScreenSize = game.Workspace.CurrentCamera.ViewportSize
local MouseViewport = Vector2.new(X / ScreenSize.X, Y / ScreenSize.Y)
local Ray_ = workspace.CurrentCamera:ViewportPointToRay(MouseViewport.X, MouseViewport.Y)
local RayResult = workspace:Raycast(Ray_.Origin, Ray_.Direction * 2000)

that meen no object was hit by the ray :face_with_monocle:

But actually I’m trying to hit a dummy, and it worked when i didn’t add screensize. still, it was inaccurate

local MousePos = UIS:GetMouseLocation()
local screenSize = workspace.CurrentCamera.ViewportSize

local MousePosViewport = Vector2.new(MousePos.X / screenSize.X, MousePos.Y / screenSize.Y)

local Ray = workspace.CurrentCamera:ViewportPointToRay(MousePosViewport.X, MousePosViewport.Y)

local RayResult = workspace:Raycast(Ray.Origin, Ray.Direction * 2000)

if RayResult then
	script.Shoot:FireServer(Tool.Name, {X = RayResult.Position.X; Z  = RayResult.Position.Z; Y = RayResult.Position.Y})
else
	print("No object was hit by the raycast")
end

If not work i dont know what to do more

I meant :ViewportPointToRay(MousePos.X, MousePos.Y - 36)
(Second line you shared), MousePos is already measured in pixels

Answer

You can not fix mouse position because mouse position is not a property in Roblox. You can get the mouse position with the following code:
local mousePosition = game.Players.LocalPlayer:GetMouse().Hit
local mouseRay = workspace.CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local mouseTarget = mouseRay.Origin + mouseRay.Direction * 9999

This will give you a vector3 that you can use to shoot at.

Answer

The following script will work best for you, it will give you a ray and give you a result.

local MousePos = UIS:GetMouseLocation()
local Ray_ = workspace.CurrentCamera:ViewportPointToRay(MousePos.X, MousePos.Y)
local RayResult = workspace:Raycast(Ray_.Origin, Ray_.Direction * 2000)

if RayResult then
script.Shoot:FireServer(Tool.Name, {X = RayResult.Position.X; Z = RayResult.Position.Z; Y = RayResult.Position.Y})
end

I hope this script will help you out.

Answer

local mouse = game.Players.LocalPlayer:GetMouse()
local target = mouse.Target
local mouseRay = workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
local mouseTarget = mouseRay.Origin + mouseRay.Direction * 9999
local mousePosition = mouseRay.Origin + mouseRay.Direction * mouse.Hit.magnitude

if target and target:IsA(“BasePart”) then
mousePosition = target.Position
end

This should work a lot better for you.

How do I make a game like Bloxburg but with a different name for it?

Answer

I’m not sure what you mean, but, if you mean something like this, you can use a tool named “Game Explorer”.

With the tool, you can check out the properties, scripts, and other things related to the game.

All you need is a tool like this and a vast knowledge of Lua, which you can gain by searching the web for tutorials and reading the wiki.

However