Issue with getting the mouse's position

I am trying to get the mouse’s position. But it keeps breaking on me.

For some reason I always make an argument while firing the server and that argument just means nothing to the script but just the mouse. I tried hit, target, getting the X and Y position of the mouse, I will send both my scripts and the parenting of the tool. I have tried to make it as simplified as I can to get rid of any unnecessary things.

LocalScript (client):

local tool = script.Parent

local plr = game.Players.LocalPlayer

local mouse = game.Players.LocalPlayer:GetMouse()

local OnFired = tool:WaitForChild("OnFired")

local mousePos = mouse.Hit.Position
--activate gunshots

tool.Activated:Connect(function()
	OnFired:FireServer(plr, mouse)
	print(mousePos)
end)

ServerScript ( Server ) :

local tool = script.Parent

local ServerEvent = tool:WaitForChild("OnFired")

ServerEvent.OnServerEvent:Connect(function(plr, mouse)
	local mousePos = mouse.Hit.Position 
	print(mousePos)
end)

Parenting ( AK47) :
Capture

Anything helps.

2 Likes

When firing the server you don’t need to include a player object. The player object is automatically sent to the server as the first parameter. Also you can’t send the mouse itself to the server. You need to send the mouse hit position only.

tool.Activated:Connect(function()
	OnFired:FireServer(plr, mouse.Hit.Position) -- Should be OnFired:FireServer(mouse)
	print(mousePos)
end)
ServerEvent.OnServerEvent:Connect(function(plr, mousePos)
	print(mousePos)
end)
3 Likes

I agree with you. What your saying makes a lot of sense.

2 Likes

It works, but it only prints one position. How can I make sure its printing in the position its at?

1 Like

I don’t understand the question?

1 Like

Well, I click once and it prints the position. So it works, but the problem is that every time I fire the event, it only prints the first position it was at. I click in every other direction and its still the position I clicked.

1 Like

You Send To The Server 3 args Player by Default and Player agen and the mouse position

2 Likes

You can’t pass the client’s mouse object through a RemoteEvent, consider passing mouse.Hit or mouse.Hit.Position instead.

OnFired:FireServer(mouse.Hit.Position)
ServerEvent.OnServerEvent:Connect(function(plr, mousePosition)
1 Like

I don’t understand what this means.

You need to put the mousePos inside the activated event so it has the most accurate.

local tool = script.Parent

local plr = game.Players.LocalPlayer

local mouse = game.Players.LocalPlayer:GetMouse()

local OnFired = tool:WaitForChild("OnFired")

local mousePos = mouse.Hit.Position
--activate gunshots

tool.Activated:Connect(function()
   local mousePos = mouse.Hit.Position

	OnFired:FireServer(plr, mousePos)
	print(mousePos)
end)
2 Likes

It should send the most updated mouse position each time. Did you use all of the code I wrote?

1 Like

Yes. I got it to work from @SilentSuprion

1 Like

Their code is effectively the same as mine.

1 Like

So it works, which helps a lot, but it looks… Fairly similar to my script, what changed?

1 Like

Yea idk why it’d be working for his but not yours?

I suggested the same changes in a previous reply (I just didn’t provide the full code snippet in the hopes that you’d try to implement the changes yourself).

when fire remote event from client to Server You Will sent player as argment By Default
Even IF you don’t sent anything

1 Like

Oh, so I don’t need to put the player. Noted

1 Like

Tried it, I am still stuck on why one person’s script worked but not the others WHICH were similar.

Also use:

local player = game:GetService("Players").LocalPlayer

It’s better practice.

2 Likes