Attempt To Index Nil with Mouse.Target?

Hey everyone! I was testing a keybinded function where when you press “E”, it would fire the remote event, and give you the mouse’s target.

And I got an error of how its indexing nil with Mouse.Target. Here is my script:

local rs = game:GetService("ReplicatedStorage")
local spin = rs:WaitForChild("DreamArt") -- A demon slayer art test

spin.OnServerEvent:Connect(function(player)
	local character = player.Character
	local humanoid = character.Humanoid
	local mouse = player:GetMouse()
	
	print(mouse.Target)
end)

You cannot use player:GetMouse() in a server script.

But you can pass mouse.Target as argument in RemoteEvent | Roblox Creator Documentation function

I added the Mouse.Target in the :FireServer() function, but how can I mention it in the server script now that i added it into the local script?

You can do it like this:

local rs = game:GetService("ReplicatedStorage")
local spin = rs:WaitForChild("DreamArt") -- A demon slayer art test

spin.OnServerEvent:Connect(function(player, target)
	local character = player.Character
	local humanoid = character.Humanoid
	print(target)
end)
2 Likes

Thanks so much, really helped! Appreciate it!

1 Like