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)
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.
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
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)
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.