Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is mouse returning nil?

hello. Im currently trying to make a part appear where your mouse is whenever you click, but remote event parameter mouse is returning nil. Why?

Local Script in StarterCharacterScripts

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

local repstorage = game:GetService("ReplicatedStorage")

local remote = repstorage:WaitForChild("RemoteEvent")

mouse.Button1Down:Connect(function()
	remote:FireServer(mouse)
end

Script in ServerScriptStorage

local repstorage = game:GetService("ReplicatedStorage")

local rp1 = rp:WaitForChild("RemoteEvent")

rp1.OnServerEvent:Connect(function(player, mouse)
	print(mouse.Hit)
end

1 answer

✓ Accepted answer

The mouse object is not replicated across the client/server boundary because the mouse is solely used by the client. Therefore, attempting to send the mouse over a RemoteEvent will result in nil because the reference is nil. What you can try instead, is to send the CFrame of the mouse as the remote argument (mouse.Hit), which is the position where the mouse was when they left-clicked. This will send the CFrame over as the argument which will not be nil.

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

local repstorage = game:GetService("ReplicatedStorage")

local remote = repstorage:WaitForChild("RemoteEvent")

mouse.Button1Down:Connect(function()
    remote:FireServer(mouse.Hit)
end
local repstorage = game:GetService("ReplicatedStorage")

local rp1 = rp:WaitForChild("RemoteEvent")

rp1.OnServerEvent:Connect(function(player, mousehit)
    print(mousehit)
end

1 comment

0

Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.