Getting current mouse position at server

So, i basically want to get curret mouse position from server to make hitboxes for my punches ability.

This is the server script

local remote = script.Parent:WaitForChild("RemoteEvent")

local TweenService = game:GetService("TweenService")

local cooldown = false

local rand = math.random

remote.OnServerEvent:Connect(function(player)
	if cooldown then return end
	cooldown = true

	local HumanoidRoot = player.Character:FindFirstChild("HumanoidRootPart")
	
	task.spawn(function()
		for i = 0, 300 do
			game.ReplicatedStorage.circle:FireAllClients(HumanoidRoot, math.random(0, 1000))
			task.wait(.1)
		end
	end)

	task.wait(1)

	cooldown = false
end)

and this is local one

local TweenService = game:GetService("TweenService")
local Sound = game:GetService("SoundService")

local circle = game.ReplicatedStorage.circle
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

local BlockSize = 2

local GoldenRatio = 1 + math.sqrt(5) / 4
local AngleIncr = math.pi * 2 * GoldenRatio
local Multipler = BlockSize * 10
local MaxPoints = 1000

local CamShaker = require(game.ReplicatedStorage.CameraShaker)

local NewCam = CamShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCFrame)
	camera.CFrame = camera.CFrame * shakeCFrame
end)

circle.OnClientEvent:Connect(function(HumanoidRoot, i)
	local Player = game.Players:GetPlayerFromCharacter(HumanoidRoot.Parent)
	local Mouse = Player:GetMouse()
	local MouseHit = Mouse.Hit
	local NewPart = Instance.new("Part")

	NewPart.Parent = workspace
	NewPart.Anchored = true
	NewPart.CanCollide = false
	NewPart.Size = Vector3.new(BlockSize, BlockSize, 1)

	local ChosenPoint = i --rand(1, MaxPoints)
	local Distance = ChosenPoint / MaxPoints
	local Angle = AngleIncr * ChosenPoint
	local Incline = math.acos(1 - 2 * Distance)
	local Azimuth = AngleIncr * ChosenPoint

	local x = HumanoidRoot.Position.X + math.sin(Incline) * math.cos(Azimuth) * Multipler
	local y = HumanoidRoot.Position.Y + math.sin(Incline) * math.sin(Azimuth) * Multipler
	local z = HumanoidRoot.Position.Z + math.cos(Incline) * Multipler

	local RandomPos = Vector3.new(x, y, z)
	NewPart.CFrame = CFrame.new(RandomPos, MouseHit.Position)

	local MouseMag = (MouseHit.Position - RandomPos).Magnitude

	--if MouseMag > 500 then return end

	local Tween = TweenService:Create(NewPart, TweenInfo.new(.8, Enum.EasingStyle.Quint, Enum.EasingDirection.In), {
		Size = Vector3.new(BlockSize, BlockSize, MouseMag),
		CFrame = NewPart.CFrame * CFrame.new(0, 0, -MouseMag / 2)
	})

	local TheOtherTween = TweenService:Create(NewPart, TweenInfo.new(.9, Enum.EasingStyle.Quint), {Transparency = 1})

	Tween:Play()

	task.delay(3, function()
		TheOtherTween:Play()
		task.wait(1.2)
		NewPart:Destroy()
	end)

	Tween.Completed:Connect(function()
		local PlayerItselfChar = player.Character
		local Root = PlayerItselfChar:FindFirstChild("HumanoidRootPart")
		local Mag = (Root.Position - MouseHit.Position).Magnitude

		if Mag < 255 then
			NewCam:Start()
			NewCam:ShakeOnce(2, 10, 0, 3.2)
			Sound:PlayLocalSound(script["20mm Rifle Shot"])
		end
	end)
end)

task.wait(6)
require(game.ReplicatedStorage.Paranoia).Inject()

im making the tweens and parts at the clientside so the player doesnt lag

1 Like

Unfortunately, there is not a method to get the current mouse position with the server, you need to use the client to send the mouse position to the server and have the server send that information to the client. Be wary if you are using mouse position, though, since clients can pick any angle at all to shoot. Adding some sanity checks should help with that.

can i use remote functions to get the current mouse pos if i can would it be efficient

I think the best method would be to add the mouse position to all the variables. e.g:

circle.OnClientEvent:Connect(function(HumanoidRoot, i, MouseHit)
	local Player = game.Players:GetPlayerFromCharacter(HumanoidRoot.Parent)
	local NewPart = Instance.new("Part")

	NewPart.Parent = workspace
	NewPart.Anchored = true
	NewPart.CanCollide = false
	NewPart.Size = Vector3.new(BlockSize, BlockSize, 1)

	local ChosenPoint = i --rand(1, MaxPoints)
	local Distance = ChosenPoint / MaxPoints
	local Angle = AngleIncr * ChosenPoint
	local Incline = math.acos(1 - 2 * Distance)
	local Azimuth = AngleIncr * ChosenPoint

	local x = HumanoidRoot.Position.X + math.sin(Incline) * math.cos(Azimuth) * Multipler
	local y = HumanoidRoot.Position.Y + math.sin(Incline) * math.sin(Azimuth) * Multipler
	local z = HumanoidRoot.Position.Z + math.cos(Incline) * Multipler

	local RandomPos = Vector3.new(x, y, z)
	NewPart.CFrame = CFrame.new(RandomPos, MouseHit.Position)

	local MouseMag = (MouseHit.Position - RandomPos).Magnitude

	--if MouseMag > 500 then return end

	local Tween = TweenService:Create(NewPart, TweenInfo.new(.8, Enum.EasingStyle.Quint, Enum.EasingDirection.In), {
		Size = Vector3.new(BlockSize, BlockSize, MouseMag),
		CFrame = NewPart.CFrame * CFrame.new(0, 0, -MouseMag / 2)
	})

	local TheOtherTween = TweenService:Create(NewPart, TweenInfo.new(.9, Enum.EasingStyle.Quint), {Transparency = 1})

	Tween:Play()

	task.delay(3, function()
		TheOtherTween:Play()
		task.wait(1.2)
		NewPart:Destroy()
	end)

	Tween.Completed:Connect(function()
		local PlayerItselfChar = player.Character
		local Root = PlayerItselfChar:FindFirstChild("HumanoidRootPart")
		local Mag = (Root.Position - MouseHit.Position).Magnitude

		if Mag < 255 then
			NewCam:Start()
			NewCam:ShakeOnce(2, 10, 0, 3.2)
			Sound:PlayLocalSound(script["20mm Rifle Shot"])
		end
	end)
end)

And on the server:

remote.OnServerEvent:Connect(function(player,MouseHit)
	if cooldown then return end
	cooldown = true

	local HumanoidRoot = player.Character:FindFirstChild("HumanoidRootPart")
	
	task.spawn(function()
		for i = 0, 300 do
			game.ReplicatedStorage.circle:FireAllClients(HumanoidRoot, math.random(0, 1000),MouseHit)
			task.wait(.1)
		end
	end)

	task.wait(1)

	cooldown = false
end)

And wherever you are sending the remote in the first place:

remote:FireServer(Mouse.Hit)

Tell me if you run into any issues. I’m not fact checking lol

lua remote.OnServerEvent:Connect(function(player,MouseHit) that MouseHit is the old mouse position but i want to get current mouse position at server

What do you mean? Do you want all the players mouse position, or the position after it finishes?

i mean i dont want to use constant mouse position i want to get current mouse position at the “for” loop

Ah, I see. I think you will need to use RemoteFunctions, but those do delay the for loop, although I don’t think it will be too noticeable. I don’t often use RemoteFunctions, though.