Local Script to Server Script Mouse Returning Nil

Hello,

I’m trying to make a singleplayer game with a custom tool system. I have two scripts, a client script and a server script.
The client script fires a remote event when F is pressed which the server script reads. Here’s the client script:

local plr = game:GetService("Players").LocalPlayer
local uis = game:GetService("UserInputService")
local mse = plr:GetMouse()

uis.InputBegan:Connect(function(i, g)
	if not g then
		if i.KeyCode == Enum.KeyCode.F then	
			game.ReplicatedStorage.EquipToolsEvent:FireServer(mse, mse.Target)
		end
	end
end)

and then the server script receives this and attempts to pick up the object:

local evt = game.ReplicatedStorage:WaitForChild("EquipToolsEvent")
local etp = script.Parent:WaitForChild("EquippedToolPos")

evt.OnServerEvent:Connect(function(plr, mse, tgt)
	print(tgt)
	if tgt then
		if tgt.Parent.Name == "Tools" and etp.Parent.ToolEquipped.Value == false then
			local tool = tgt
			local weld = Instance.new("WeldConstraint",etp)
			etp.Parent.ToolEquipped.Value = true
			tool.Parent = etp
			tool.CFrame = etp.CFrame
			tool.CanCollide = false
			weld.Part0 = etp
			weld.Part1 = tool
		else
			if etp.Parent.ToolEquipped.Value == true then
				for i,v in pairs(etp:GetChildren()) do
					if v:IsA("BasePart") or v:IsA("MeshPart") then
						v.Parent = workspace.Tools
						v.CanCollide = true
						etp:ClearAllChildren()
						etp.Parent.ToolEquipped.Value = false
					end
				end
			else
				etp:ClearAllChildren()
			end
		end
	end
end)

I’ve tried disabling other scripts and editing several parts of my code and this didn’t help.

Thanks!

Since the mouse is only there for the client, it cannot be passed along a RemoteEvent. A detour could be using the mouse’s CFrame property instead.

game.ReplicatedStorage.EquipToolsEvent:FireServer(mse.Hit, mse.Target)
1 Like

okay… but then how could I use this for getting the objects Name or Instance?

well, it looks like you aren’t even using the mse variable in the script anyway.
Does that print(tgt) statement print nil?

yeah, I have mse in case tgt doesn’t work, but either way through server scripts, you can’t do mse.Target;
not sure what to do now.

Is the Target variable working? As in, is it passing correctly and not ending up as nil?

The Target variable isn’t working; it is passing nil.

Try passing the target’s Name, CFrame, or something it can be identified by server-side.

Trying soon! setting up a server at the moment

Shows an error:

Are you checking for a target?
You could do:

if mouse.Target then
    print(mouse.Target.Name)
end

yeah, it returns the error i had

okay so i found a short term fix, i need to disable some code that allows player head movement

1 Like

Can you send that code? Just want to have a look at it to see if anything might be messing with the other code.

nevermind, i ended up fixing it myself, i accidentally added a TargetFilter to the mouse!

1 Like

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