My ServerEvent wont let me find the position of the mouse

Heyguys, I am learning about remote events and have been hitting a mistake recently. everytime I try to spawn a part on the position of the mouse, it doesnt work.

I’ve tried putting the lines in the local script, but I dont know if its possible to transfer a variable/information from the local script where its firing, into the server script that detects it.

The error code I get is: attempt to index nil with 'Hit' , and I dont know what im doing wrong.

Local script:

local InvEvent = game:GetService("ReplicatedStorage"):WaitForChild("InvisEvent")
player = game:GetService("Players").LocalPlayer

tool.Activated:Connect(function()
	InvEvent:FireServer()
end)

Server Script:

local replicatedstorage = game:GetService("ReplicatedStorage")
local InvEvent = Instance.new("RemoteEvent")
InvEvent.Name = "InvisEvent"
InvEvent.Parent = replicatedstorage
local tool = game.StarterPack.invisible
local spawnable = Instance.new("Part")


InvEvent.OnServerEvent:Connect(function(player)
	local mouse= player:GetMouse()
	local mouseposition = mouse.Hit.Position
	spawnable.Position = mouseposition
	spawnable.Parent = workspace
end)

If anyone can help me, or maybe explain something Im doing wrong, any help is appreciated.

Server-Scripts can’t get the mouse location of player instead you will have to give the Server the Mouse position.

Local script:

local InvEvent = game:GetService("ReplicatedStorage"):WaitForChild("InvisEvent")
local player = game:GetService("Players").LocalPlayer

tool.Activated:Connect(function()
	InvEvent:FireServer(player:GetMouse().Hit.Position)
end)

Sever-Script:

local replicatedstorage = game:GetService("ReplicatedStorage")
local InvEvent = Instance.new("RemoteEvent")
InvEvent.Name = "InvisEvent"
InvEvent.Parent = replicatedstorage
local tool = game.StarterPack.invisible
local spawnable = Instance.new("Part")


InvEvent.OnServerEvent:Connect(function(player, mouseposition)

	spawnable.Position = mouseposition
	spawnable.Parent = workspace
end)
1 Like

So, would mouseposition be all the data you sent through the fireserver function?

The player that fired the remote is also automatically sent through the remote.

1 Like

I dont understand, so just what is mouseposition? is it the result of player:GetMouse().Hit.Position??

Yes.

1 Like

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