Why can't I access mouse.Target in a server script?

Hello there! I’m trying to make a script that gets the mouse.Target when the player chats a certain work, but for some reason its saying that I’m indexing nil with mouse.Target.

How can I get this script to work? all help is appreciated

here’s the script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debounce = false

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		Player.Chatted:Connect(function(msg)
			if msg == "Immobilus" and not debounce then
				print("working")
				local witch = Character:FindFirstChild("Witch")
				if witch.Value == true then
					print("is a witch")
					local magic = Character:FindFirstChild("Magic")
					if magic.Value > 60 then
						print("enough magic")
						
						debounce = true
						magic.Value -= 60
						
						local mouse = Player:GetMouse()
						local target = mouse.Target

						print(mouse.Target)
						
					end
					print("debounce")
					wait(5)
					debounce = false
					print("can work now")
				end
			end
		end)
	end)
end)
1 Like

The mouse is a client side thing, simple fact is that you can’t access it from the server. You’ll have to use userinputservice and a remote event (or remote function) to get stuff from the mouse on the client to the server.

2 Likes

mouse must be in a local script since clients have their own mouse, so if you want to access it in a server script you can use remote events.

1 Like

there is nothin such as a server mouse. u must access it from a local script, since each plr has a different mouse.

1 Like

It can’t be accesed a player’s mouse from server. Just client.

1 Like

Here’s how I’d do it:

  1. Local script in starter character scripts and a remote event in replicated storage
  2. Get the player’s mouse in the local script and pass it to the server as a parameter.
  3. Receive it on the server. You now have the player’s mouse.

You’ll have to change the server script a bit:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debounce = false

ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, mouse)
	local Char = plr.Character or plr.CharacterAdded:Wait()
        local Humanoid = Char:WaitForChild("Humanoid")
	Player.Chatted:Connect(function(msg)
		if msg == "Immobilus" and not debounce then
			print("working")
			local witch = Character:FindFirstChild("Witch")
			if witch.Value == true then
				print("is a witch")
				local magic = Character:FindFirstChild("Magic")
				if magic.Value > 60 then
					print("enough magic")
					
					debounce = true
					magic.Value -= 60
					local target = mouse.Target
					print(mouse.Target)		
				end
				print("debounce")
				wait(5)
				debounce = false
				print("can work now")
			end
		end
	end)
end)

1 Like

The Mouse is managed by the Client, so if you wanted to get the target of the mouse you had to use a RemoteEvent or methods like that.

Thank you for this! I’m gonna try it out now : )