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

Why does this keep saying mouse is a nil value?

I am trying to use the mouse in a server script and I am using a remote event to get the mouse but it keeps saying that it is a nil value

-- (Part of) Server Script
local RS = game:GetService("ReplicatedStorage")
local getPlayerMouse = RS:WaitForChild("GetPlayerMouse")

Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	mouse = getPlayerMouse:FireClient(player)
end)

. . .

mouse.KeyDown:Connect(function(key)
	if key == "q" then
		cancelPlacement()
	end
end)
-- Local script
local RS = game:GetService("ReplicatedStorage")
local remote = RS:WaitForChild("GetPlayerMouse")

remote.OnClientEvent:Connect(function()
	local plr = game:GetService("Players").LocalPlayer
	local mouse = plr:GetMouse()
	return mouse
end)

1 answer

✓ Accepted answer

The Mouse object is a non-replicated instance and can only be seen on the client themself. Since it can't be seen the server, the RemoteFunction returns nil.

You should also know that keydown is deprecated, and is not recommended to be used in new work.

What you can do, however, is to handle the key pressing on the client, and fire an event to tell it that you want to cancel placement:

-- local script

local re = game.ReplicatedStorage.SomeRemoteEvent
local uis = game:GetService('UserInputService')

uis.InputBegan:Connect(function(input, gpe)
    if gpe then return end
    if input.KeyCode == Enum.KeyCode.Q then
        re:FireServer()
    end
end)
--Server script

local re = game.ReplicatedStorage.SomeRemoteEvent

re.OnServerEvent:Connect(function(player)
    cancelplacement()
end)

1 comment

0

EYYYYYYY NICE EXPLAINING DOOBUS

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