Raycasting on a local script help

Hey guys, i’ve been struggling with an issue for quite a while now which is raycasting client-sided. I’ve managed to raycast server-sided but when i transport that code over to the client it refuses to work. I’ve read that raycasting on the client and server should make no difference code wise (apart from referencing and stuff), however i cannot manage to get it working locally. Here is my code:

local function raycast()
	local Part = script.Parent
	local RayOrigin = Part.Position
	local RayDirection = Part.CFrame.LookVector
	
	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = {Part}
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
	local RaycastResult = workspace:Raycast(RayOrigin, RayDirection, RaycastParameters)
	print("Raycasted, now waiting for result")
	
	if RaycastResult then
		local hitPart = RaycastResult.Instance
		print("hit ".. hitPart.Name)
	end
end

while wait(1) do
	raycast()
end

Nothing is printed, but i’m assuming that’s because for some reason the local script isn’t running (when i put it on the server it prints Raycasted… but not the hitpart). Can anyone enlighten me on why it isn’t running on the client and issues in the code? Thank you!

Where exactly are you placing the LocalScript? It can only run in certain areas if you weren’t aware, I’d also change the RayDirection to multiply it by 50 just to be sure it works if there’s a obstacle in the way

1 Like

It’s just in a block within the workspace. Sorry for the late reply

LocalScripts dont run under workspace.

1 Like

Thanks for the info! I’ll move it under somewhere like StarterPlayerScripts to test

Thanks to both of your help! I had to place it somewhere else and multiply it by a distance!

1 Like