Attempt to index nil with, hit

Hi, so I want to spawn a dummy/npc where the players’ mouse is pointing at when they are holding the tool. But, the issue is is that the script gets the mouse but returns attempt to index nil with hit when I try to get the position of the mouse.

Here is my current code:

--// Local Script inside tool \\

local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")

script.Parent.Activated:Connect(function()
	print("made func")
	mouse.Button1Down:Connect(function()
		print("recognized click")
		local new = dummy:Clone()
		print("cloned")
		game.ReplicatedStorage.Spawner:FireServer(plr, mouse, new)
	end)
end)

--// Server script in ServerScriptService \\
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse, new)
	local mouse = plr:GetMouse()
	new.CFrame = CFrame.new(mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z)
--The error is on this line ^^^
end)

Anyone know what I am doing wrong? Thanks!

Im sure this is the error
try this

--// Local Script inside tool \\

local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")

script.Parent.Activated:Connect(function()
	print("made func")
	mouse.Button1Down:Connect(function()
		print("recognized click")
		local new = dummy:Clone()
		print("cloned")
		game.ReplicatedStorage.Spawner:FireServer(mouse, new) -- removed plr here
	end)
end)

--// Server script in ServerScriptService \\
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse, new)
	local mouse = plr:GetMouse()
	new.CFrame = CFrame.new(mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z)
--The error is on this line ^^^
end)

when you fire a remote event to server, the first argument in OnServerEvent will be player by default, rest fired arguments will be next

ohhhh, ty i didn’t know that. i’ll try and see if this works. thanks!

and I think there is another error, give me a min.

oh ok i’ll wait until you figure it out

i believe the issue is that hes doing mouse.Hit.X and not mouse.Hit.Position.X

Im guess it’s mouse.Hit.X itself

You tried to get the Mouse in a server script.

@Earthraphobic2 try this code

--// Local Script inside tool \\

local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")

script.Parent.Activated:Connect(function()
	print("made func")
	mouse.Button1Down:Connect(function()
		print("recognized click")
		local new = dummy:Clone()
		print("cloned")
		game.ReplicatedStorage.Spawner:FireServer(mouse.Hit, new) -- removed plr here
	end)
end)

--// Server script in ServerScriptService \\
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse, new)
	local mouse = plr:GetMouse()
	new.CFrame = mouse
--The error is on this line ^^^
end)

still gives same error output “attempt to index nil with hit”

Read the post by @Pseudoisochromatic, i do believe that is the problem.

attempt to index nil with CFrame now @kittyPGR

after reading the code a bit more, there’s more errors.
He’s also sending over an instance that was created on the client. The dummy should be cloned on the server instead.

then do this

--// Local Script inside tool \\

local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")

script.Parent.Activated:Connect(function()
	print("made func")
	mouse.Button1Down:Connect(function()
		print("recognized click")
		game.ReplicatedStorage.Spawner:FireServer(mouse.Hit) -- removed plr here
	end)
end)

--// Server script in ServerScriptService \\
local dummy = workspace.Dummy
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse)
    local new = dummy:Clone()
	print("cloned")
	new.CFrame = oldmouse
--The error is on this line ^^^
end)

that’s true, the new code should have fixed that now

The code you made wont work, you cant get the players mouse on the server.

You’ll have to send over the Vector3 through the event.

game.ReplicatedStorage.RemoteEvent:FireServer(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z)

eew sorry, I forgot to remove that line, it’s useless there and would give an error

wait im confused, which code works?

--// Local Script \\
local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")

script.Parent.Activated:Connect(function()
	print("made func")
	mouse.Button1Down:Connect(function()
		print("recognized click")
		game.ReplicatedStorage.Spawner:FireServer(mouse.Hit,mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z) -- removed plr here
	end)
end)

--// Server Script \\
local dummy = workspace.Dummy
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmousex,oldmousey,oldmousez)
	local new = dummy:Clone()
	print("cloned")
	new.HumanoidRootPart.CFrame = CFrame.new(oldmousex,oldmousey,oldmousez)
	print("tped")
end)

Also why won’t this work? It is successfully printing “tped” as I commanded it to, but the dummy isn’t successfully tp’ing.

--// Local Script inside tool \\

local dummy = workspace.Dummy
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
print("established locals")

script.Parent.Activated:Connect(function()
	print("made func")
	mouse.Button1Down:Connect(function()
		print("recognized click")
		game.ReplicatedStorage.Spawner:FireServer(mouse.Hit) -- removed plr here
	end)
end)

--// Server script in ServerScriptService \\
local dummy = workspace.Dummy
game.ReplicatedStorage.Spawner.OnServerEvent:Connect(function(plr, oldmouse)
    local new = dummy:Clone()
	print("cloned")
	new.CFrame = oldmouse
--The error is on this line ^^^
end)

this code works

That just gives an error of CFrame is not a valid member of Model “Dummy”