How to fix attempt to index nil with 'GetMouse'

Why This Happens? How To Fix It?
image
image
Players = game:GetService(“Players”)

1 Like

:joy: serverscriptservice is only to be ran by serverscripts not localscripts consider changing your script to localscript and btw is that it or are you trying to do something

image
it was script, not local script -_-

1 Like

oh also do

local Players = Game:GetService("Players") -- you cant put variables all over the place
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local camera = workspace.CurrentCamera

oh i also forgot to mention only do that on local scripts and if you want to do it on server use remote events or remote functions because the server/serverscript is almost inaccessible to run anything

okay but first @NvTheDeveIoper what are you trying to do

yea but what are you trying to do exactly like what?

its like ship will move to mouse

also this is outdated go here
https://create.roblox.com/docs
youll have to find other part yourself

1 Like

? wdym ill need advanced details

Is Players defined as game.Players? if not, do this:

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()

This code works, I’ve done things alike before.

you cant get LocalPlayer on server

Then they cannot run LocalPlayer (something you tried in your reply). I’d suggest running it on a LocalScript since it’s gonna be a Gui most likely.

but hes trying to do it in serverscriptservice

And? You still can’t run LocalPlayer on a Script since it has no client exclusively to run on. If the ship part is on a Gui, use a LocalScript instead.

But if it’s 3D, then I’d want to suggest using a LocalScript to initialize, and a RemoteEvent to transfer that across the server-client boundary:

local replicatedstorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
mouse.Move:Connect(function()
	replicatedstorage.RemoteEvent:FireServer(mouse.Hit.p) --put arguments here in parenthesis
end)

On server in ServerScriptService with a Script:

local replicatedstorage = game:GetService("ReplicatedStorage")
replicatedstorage.RemoteEvent.OnServerEvent:Connect(function(player, mousespot)
	local part = workspace:FindFirstChild(player.Name .. "Ship")
	part.Position = mousespot
end)
3 Likes

Like, @daisytheghostchild98 has said,The Reason why GetMouse is nil is because this is ran on a server script inside server script service. You Can only get the mouse from the Player in a local script so what i would reccomend is switching your script from Server Script Service to Starter Player Scripts or Starter Character scripts and try there if you want this to replicate to the server you would use a remote event

2 Likes

Exactly what I said on the above post.

1 Like

it worked, thank you @Guest_player1689 and @daisytheghostchild98

2 Likes