Attempt to index nil

I have no idea how to fix this error.


It doesn’t show any errors in the script, but it shows this error

local plr = game:GetService('Players').LocalPlayer
local mouse = plr:GetMouse()

script.Parent.Activated:Connect(function()
	print'Spawn Noob'
	local AI = game.ServerStorage.Spawnables.Marine:Clone()
	AI.Parent = game.Workspace
	AI:MoveTo(script.Parent.Handle.Position + Vector3.new(mouse.hit.Position))
	AI:MakeJoints()
end)

Is this a server script by chance? The LocalPlayer is only for clients

try add task.wait(2) to the start, because the player still needs time to load in

Oh yeah it is a server script, how should I replace LocalPlayer?

Basically, you need to use a remote event If you want all your players in the server to see the part move.

You should migrate your code onto a client script and then do all the client-based things. After, anything else needed to be done on the server can be done through remote events and functions

I have no idea how to do that at all

On the client:
Handle the tool activating, getting the mouse position, and then calculating the final position. After this, send this calculated position to the server through a remote event

On the Server:
After getting the calculated position from a remote event, move the unit there

Here is a tutorial for remote events:

Server Script:

local YourRemoteEvent = ReplicatedStorage.YourRemoteEvent

YourRemoteEvent.OnServerEvent:Connect(function(Player, Position)
      print('Spawn Noob')
	local AI = game.ServerStorage.Spawnables.Marine:Clone()
	AI.Parent = game.Workspace
	AI:MoveTo(script.Parent.Handle.Position + Vector3.new(Position))
	AI:MakeJoints()
end)

Local script:

local plr = game:GetService('Players').LocalPlayer
local mouse = plr:GetMouse()
local YourRemoteEvent = ReplicatedStorage.YourRemoteEvent

script.Parent.Activated:Connect(function()
      YourRemoteEvent:FireServer(mouse.Hit.Position)
end)
1 Like

It works, but how would I get the NPC to spawn at the position where the mouse is on the server script?

Hi! I’m not sure if this will help, but this script may work.

game:GetService("Players").PlayerAdded:Connect(function(plr)

        local mouse = plr:GetMouse()

        script.Parent.Activated:Connect(function()
	        print'Spawn Noob'
	        local AI = game.ServerStorage.Spawnables.Marine:Clone()
	        AI.Parent = game.Workspace
	        AI:MoveTo(script.Parent.Handle.Position + Vector3.new(mouse.hit.Position))
	        AI:MakeJoints()
        end)
end)

This is formatted as a server script. Hope it helps!

1 Like

It’s possible that the script.Parent.Handle.Position thing is causing an offset from the intended position.

If you replace line 7 with

AI:MoveTo(Position)

does it make it go where you want?

1 Like

How would I modify this so that when I spawn the object, it goes to wherever where my mouse is, but it gets spawned up in the air?

If you want it to be a set distance up, then you can just add a Vector3 to the Position::
e.g.

AI:MoveTo(Position + Vector3.new(0, 5, 0)
1 Like

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