I have a tool and on activation in a local script it connects a function that finds the mouse.Target and the mouse Position then fires a remote event with those are parameters. I’m trying to get the target to move to the mouse’s position, i’ve done this before but this time there’s some issues.
I originally had the scripts parented to the tool but I moved the local script to starter Character and the server script to serverscriptservice to see if it worked, and it didn’t. I also changed some variables into global variables to see if it would work, it didn’t so i changed them back.
The target is correctly identified in the local script, but not the server script.
Here is my local script:
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local tool = player.Backpack.Tool
local connection
local active = false
mouse.TargetFilter = player
tool.Activated:Connect(function()
active = true
local target = mouse.Target
connection = runService.Heartbeat:Connect(function()
if active == true then
remoteEvent:FireServer(target, mouse.Hit.Position)
else
connection:Disconnect()
end
end)
end)
tool.Deactivated:Connect(function()
active = false
connection:Disconnect()
end)
Here is my Server Script:
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")
remoteEvent.OnServerEvent:Connect(function(player, target, mousePos)
if target == nil then
print("Not working")
end
end)
Does an error appear in console? I did have to add a few variables like ‘runService’ to get it to work.
The only other thing I see is within the localscript, you may want to move the “target” variable declaration within the heartbeat event, that way the target updates on each heartbeat. Currently it would only show the first target on activation while repeatedly firing the event.
Like this:
tool.Activated:Connect(function()
active = true
connection = runService.Heartbeat:Connect(function()
if active == true then
local target = mouse.Target
remoteEvent:FireServer(target, mouse.Hit.Position)
else
connection:Disconnect()
end
end)
end)
The target updates correctly on the local script each heartbeat loop, and I can’t have the target constantly updating because it’s supposed to be a mouse drag thing. I tested out your script but when i tried to print Target.Parent in the server script it’s still giving me an error called,
ServerScriptService.Script:6: attempt to index nil with ‘Parent’
Alright, If you don’t want it updating constantly you can ignore my previous suggestion (:
This is my version of the Server Script so far, which seems to be working. (It prints the parent of whatever I left clicked on, until I let go)
Check to see if you have any differences
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")
remoteEvent.OnServerEvent:Connect(function(player, target, mousePos)
if target == nil then
print("Not working")
else
print(target.Parent)
end
end)
It all of a sudden just started working, I set the target filter of the mouse to nil after the tool.Deactivated event like you did. It might’ve been that or something with roblox, regardless thank you for your help. I’ll mark your answer as the solution.
And to answer your question, I just had the local script print mouse.Target and had the server do the same thing. Output looked like this: (Part), (nil)