Having problem with getting the mouse Target from the client with FireServer

I’m trying to transfer the object value corresponding to a part between the client/server boundary using a tool. On the client, I have a script that gets the part that the cursor is clicking on. The client script then fires a RemoteEvent stored in the tool that should pass the value for that part to the server. However when the server script receives that event, it instead receives the player, not the part that was clicked on, even though I specifically set it to return the part. I can’t figure out why it does this, especially considering it otherwise sends Mouse.Hit.Position just fine - it just sends an entirely different value when I try to use the RemoteEvent to send Mouse.Target???

LocalScript:

local Tool = script.Parent
local Players = game:GetService("Players")
local MouseLoc = Tool:WaitForChild("MouseLoc") -- This is a remoteEvent

function onActivated()
	local myPlayer = Players:GetPlayerFromCharacter(script.Parent.Parent)
	local mouseTarget = myPlayer:GetMouse().Target
	print("This is a Mouse Target! It should not return a Player's Character.")
	print(tostring(mouseTarget).. " This is the name of the part then.")
	print(tostring(mouseTarget.ClassName).. " This is the class name string then.")
	print("You see? After this message it shouldn't transmute into a Player! It should be a Part.")
	MouseLoc:FireServer(myPlayer,mouseTarget)
end

script.Parent.Activated:Connect(onActivated)

ServerScript:

local Tool = script.Parent
local MouseLoc = script.Parent:FindFirstChild("MouseLoc")

function onActivated(player, targetBrick)
	print(tostring(targetBrick).. " This is the name of the part now.")
	print(tostring(targetBrick.ClassName).. " This is the class name string now.")
	wait(1.0)
	Tool:remove()
end

script.Parent.MouseLoc.OnServerEvent:Connect(onActivated)

Output:

1 Like

In your local script where it says:

MouseLoc:FireServer(myPlayer,mouseTarget)

that isn’t right because when you fire the server you don’t need to give the player as a value because it does it automatically. so instead should look like this:

MouseLoc:FireServer(mouseTarget)

hopefully that helps a little because I’m not sure I understand this issue fully.

2 Likes

I added the part where it gets the player earlier because some scripts called for that while I was testing it. Adding it though, did nothing to fix the issue, and removing it now still does nothing - this is the output


The problem I’m having here, is that the RemoteEvent is returning a player instead of the part that was clicked on in the client.

Can you give the local script again now with the updates?

1 Like

LocalScript:

local Tool = script.Parent
local Players = game:GetService("Players")
local MouseLoc = Tool:WaitForChild("MouseLoc") -- This is a remoteEvent

function onActivated()
	local myPlayer = Players:GetPlayerFromCharacter(script.Parent.Parent)
	local mouseTarget = myPlayer:GetMouse().Target
	print("This is a Mouse Target! It should not return a Player's Character.")
	print(tostring(mouseTarget).. " This is the name of the part then.")
	print(tostring(mouseTarget.ClassName).. " This is the class name string then.")
	print("You see? After this message it shouldn't transmute into a Player! It should be a Part.")
	MouseLoc:FireServer(mouseTarget)
end

script.Parent.Activated:Connect(onActivated)

ServerScript

local Tool = script.Parent
local MouseLoc = script.Parent:FindFirstChild("MouseLoc")

function onActivated(targetBrick)
	print(tostring(targetBrick).. " This is the name of the part now.")
	print(tostring(targetBrick.ClassName).. " This is the class name string now.")
	wait(1.0)
	Tool:remove()
end

script.Parent.MouseLoc.OnServerEvent:Connect(onActivated)

change just the server script to this:

local Tool = script.Parent
local MouseLoc = script.Parent:FindFirstChild("MouseLoc")

function onActivated(player, targetBrick)
	print(tostring(targetBrick).. " This is the name of the part now.")
	print(tostring(targetBrick.ClassName).. " This is the class name string now.")
	wait(1.0)
	Tool:remove()
end

script.Parent.MouseLoc.OnServerEvent:Connect(onActivated)

In this script of your’s you are making a mistake in your local script. There is no need to send the player as well because it is by default available. Removing this may fix the issue.

MouseLoc:FireServer(mouseTarget)

I am also going to suggest you not to use mouse.target as it is quite ineffective and unreliable, rather, use raycasts which are more powerful and reliable.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.