Why is this not working?

i swear i’ve used something like this before, for a ray atleast, to cast it to the mouses postion.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")


local EquipBlock = ReplicatedStorage:FindFirstChild("EquipBlock")

players.PlayerAdded:Connect(function(player)
	local blocksFolder = Instance.new("Folder", player)
	blocksFolder.Name = "Blocks"
end)

EquipBlock.OnServerEvent:Connect(function(player, block)
	local mouse = player:GetMouse()
	local raycast = Ray.new(mouse.Hit.X, mouse.Hit.Y)
	print(raycast.Origin,",", raycast.Direction)
end)

heres the code, genuinely dont know what im doing wrong. local raycast = Ray.new(mouse.Hit.X, mouse.Hit.Y), thats what errors

thank you

You’re calling .OnServerEvent which means this (should) be running on the server. You then try to call Player:GetMouse() which only works on the client (i.e. local scripts).

To accomplish what you’re trying to in the above script, you’d need to send the relevant Mouse data from the client to the server as an argument.

Generic Example:

-- Local Script 
local player = game.Players.LocalPlayer
local moues = player:GetMouse()

local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent:FireServer(mouse.Hit.Position)


-- Server Script
local remote = game.ReplicatedStorage.RemoteEvent
remote.OnServerEvent:Connect(function(player, mousePos)
     print(mousePos) -- Prints a Vector3
end)

1 Like

This is correct. Additionally, try switching to workspace:Raycast() instead of Ray.new() as it’s less efficient.

EDIT: would be something similar to this:

workspace:Raycast(mouse.Hit.X, mouse.Hit.Y) -- Origin X, Direction Y
1 Like
--!strict
local raycast = workspace.Raycast
--raycast(workspace,...)

Will be even faster btw

1 Like

Try structuring it differently; Lets use LookVector of camera instead of mouse?

Server:

--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")


local EquipBlock:RemoteEvent = ReplicatedStorage.EquipBlock

local function added(plr:Player):()
	local blocksFolder = Instance.new("Folder")
	blocksFolder.Name = "Blocks"
	blocksFolder.Parent = plr
end
Players.PlayerAdded:Connect(added)

for i,v in Players:GetPlayers()
	added(v)
end

local raycast = workspace.Raycast
EquipBlock.OnServerEvent:Connect(function(player:Player, LookVector:Vector3|any):()
	if typeof(LookVector)~="Vector3" then return end
	local ray = raycast(workspace,player.Character.Head.Position,LookVector.Unit*100)
	print(ray.Origin,",", ray.Direction)
end)