I can't get mouse.Target in the server script

Local Script:

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local event = game:GetService("ReplicatedStorage"):WaitForChild("Events").Click

mouse.Button1Down:Connect(function()
	event:FireServer(player)
	--local t = mouse.Target
	--local block = t.Name
	--if mouse.Target ~= nil and block == "testBlock" then   
	--	print("-Hp")
	--	t:WaitForChild("Health").Value -= 1
	--end
end)

Server:

local event = game:GetService("ReplicatedStorage"):WaitForChild("Events").Click

event.OnServerEvent:Connect(function(player)
	local mouse = player:GetMouse()
	
	local t = mouse.Target
	local block = t.Name
	if mouse.Target ~= nil and block == "testBlock" then   
		event:FireServer(player, mouse)
		print("-Hp")
		t:WaitForChild("Health").Value -= 1
	end
end)

So I tried to get Target in the script, but for some reason it gives an error, why?

You can only get the mouse from the client side so this won’t work. You could pass the instance through the server event instead.

1 Like

:GetMouse() is a client-side only method – using it on the server won’t work. You’d also need to use a RemoteEvent or RemoteFunction to communicate between the client-server.

3 Likes

You could try something that looks like this.

SERVER SIDE

event.OnServerEvent:Connect(function(player,Block,Damage)
	
	if  Block.Name  == "testBlock" then   
		
		Block:WaitForChild("Health").Value -= Damage --In this case 1, which you can set it to 1 on client side
	end
end)
1 Like

Client side:



local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local event = game:GetService("ReplicatedStorage"):WaitForChild("Events").Click
local Damage = 1

mouse.Button1Down:Connect(function()

if mouse.Target then -- I guess you could check if the block's name is correct on the client side instead 
 of the server side.

 event:FireServer(player,mouse.Target,Damage )

  end


end)
1 Like

Okay, I did this:
Local:

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local event = game:GetService("ReplicatedStorage"):WaitForChild("Events").Click
local Damage = 1

mouse.Button1Down:Connect(function()
	
	if mouse.Target then
		print("client")
		event:FireServer(player,mouse.Target,Damage )
	end
end)

Server:

local event = game:GetService("ReplicatedStorage"):WaitForChild("Events").Click

event.OnServerEvent:Connect(function(player,Block,Damage)
	print("server1")
	print(Block.Name)
	if  Block.Name  == "testBlock" then   
		print("server2")
		Block:WaitForChild("Health").Value -= Damage
	end
end)

But as you can see the check does not pass

It looks like your print statement is saying the mouse targets name is “Dssall2” meaning the check won’t pass.

So I understand that, I have a question, why does he think he is Dssall2? And not testBlock. If I click on testBlock?

I’m not home currently so I can’t test but if I had to guess you’re somehow clicking yourself “Dssall2” instead of “testBlock”

Are you doing this in first person?

You are firing the RemoteEvent with your player as an argument, which messes up the arguments on the server-side.

Remove player, on the line event:FireServer(player,mouse.Target,Damage )

1 Like

No, I do it from the third person, and I press exactly on the block

Oh yes I completely forgot RemoteEvents automatically send the player argument through.

@XSiggeManx is correct.

char limit

Local:

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local event = game:GetService("ReplicatedStorage"):WaitForChild("Events").Click
local Damage = 1

mouse.Button1Down:Connect(function()
	
	if mouse.Target then
		print("client")
		event:FireServer(mouse.Target,Damage )
	end
end)

Server:

local event = game:GetService("ReplicatedStorage"):WaitForChild("Events").Click

event.OnServerEvent:Connect(function(Block,Damage)
	print("server1")
	print(Block.Name)
	if  Block.Name  == "testBlock" then   
		print("server2")
		Block:WaitForChild("Health").Value -= Damage
	end
end)

Okay, I did like this, but it still prints the player

You still need to keep the player argument on the server connection

The first argument of your RemoteEvent is still the player, change it to player, Block, Damage.

1 Like

Huh thank you two for your help)

1 Like

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