Roblox Forum banner

How i can updating mouse position?

4 reading
1.5K views 1 reply 2 participants last post by  CallMeKY  
#1 ·
Hi there!
I'm need to always get mouse position when player pressing X.
How i can do that with RemoteEvent?

Here my code:
[CODE lang="lua" title="LocalScript"]-- Events
local PlacePCE = game.ReplicatedStorage.PlacePC_Event
local PlaceCE = game.ReplicatedStorage.PlaceC_Event
-- Services
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
if UIS:GetFocusedTextBox() then
return
end

if input.KeyCode == Enum.KeyCode.X then
local MouseTF = game.Players.LocalPlayer:GetMouse().TargetFilter
local MouseHP = game.Players.LocalPlayer:GetMouse().Hit.Position
PlacePCE:FireServer(MouseTF, MouseHP)
end
end)[/CODE]
[CODE lang="lua" title="Script"]local isPlacingPC = false

local function PlacingPC(player, MouseTF, MouseHP, input)
local PreviewCube = game.ReplicatedStorage.Cube:Clone()
PreviewCube.Parent = game.Workspace["Build_".. player.Name]
PreviewCube.Name = "PreviewCube"

MouseTF = PreviewCube

isPlacingPC = true
while isPlacingPC == true do
PreviewCube.Position = MouseHP
wait(0.05)
end
end

game.ReplicatedStorage.PlacePC_Event.OnServerEvent:Connect(PlacingPC)[/CODE]
1646237103484.png
 
#2 ·
Hi there!
I'm need to always get mouse position when player pressing X.
How i can do that with RemoteEvent?

Here my code:
[CODE lang="lua" title="LocalScript"]-- Events
local PlacePCE = game.ReplicatedStorage.PlacePC_Event
local PlaceCE = game.ReplicatedStorage.PlaceC_Event
-- Services
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
if UIS:GetFocusedTextBox() then
return
end

if input.KeyCode == Enum.KeyCode.X then
local MouseTF = game.Players.LocalPlayer:GetMouse().TargetFilter
local MouseHP = game.Players.LocalPlayer:GetMouse().Hit.Position
PlacePCE:FireServer(MouseTF, MouseHP)
end
end)[/CODE]
[CODE lang="lua" title="Script"]local isPlacingPC = false

local function PlacingPC(player, MouseTF, MouseHP, input)
local PreviewCube = game.ReplicatedStorage.Cube:Clone()
PreviewCube.Parent = game.Workspace["Build_".. player.Name]
PreviewCube.Name = "PreviewCube"

MouseTF = PreviewCube

isPlacingPC = true
while isPlacingPC == true do
PreviewCube.Position = MouseHP
wait(0.05)
end
end

game.ReplicatedStorage.PlacePC_Event.OnServerEvent:Connect(PlacingPC)[/CODE]
View attachment 64634
GetMouse is pretty outdated and was even deprecated for a while, I'd suggest switching to UserInputService for stuff like that. As long as I'm correctly understanding what you're trying to do, here's an example of how to update 3D mouse position on the server while X is held.

Code:
local event = game:GetService('ReplicatedStorage'):WaitForChild('PlacePC_Event', 5)

local players = game:GetService('Players')
local userInputService = game:GetService('UserInputService')
local camera = workspace.CurrentCamera
local xHeld = false

-- use raycasting to translate 2d mouse position to 3d
local function getMousePosition3D(mousePosition2D)
    local camRay = camera:ViewportPointToRay(mousePosition2D.X, mousePosition2D.Y)
    local raycastResult = workspace:Raycast(camRay.Origin, camRay.Direction * 1000, RaycastParams.new()) -- you can easily customize raycast params if you want more control over filtering or anything
    if not raycastResult then return end
    return raycastResult.Position
end

-- update mouse position on the server only if raycast result is not nil
local function updateMousePosition()
    local mousePosition3D = getMousePosition3D(userInputService:GetMouseLocation())
    if not mousePosition3D then return end
    event:FireServer(mousePosition3D)
end

-- if x is pressed, set xHeld to true and try to update mouse position
userInputService.InputBegan:Connect(function(input, onGui)
    if input.KeyCode == Enum.KeyCode.X and not onGui then
        xHeld = true
        updateMousePosition()
    end
end)

-- if x is released, set xHeld to false
userInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.X then
        xHeld = false
    end
end)

-- whenever mouse movement is detected, try to update mouse position if x is held
userInputService.InputChanged:Connect(function(input, onGui)
    if input.UserInputType == Enum.UserInputType.MouseMovement and xHeld and not onGui then
        updateMousePosition()
    end
end)