Help with Raycast

I’ve recently returned to scripting after taking another longish break and I need a little assistance. I am trying to do some simple raycasting on the client with OOP and I’m pretty sure I messed up a lot of things.

Here is the module code:

local ClientModule = {}

ClientModule.__index = ClientModule

function ClientModule.new()
	local self = setmetatable({}, ClientModule)
	return self
end


function ClientModule:Init(Char)
	self.Character = Char:GetChildren()
end

function ClientModule:Cast(Start, End)
	local Origin = Start
	local Direction = End
	
	local RaycastParamaters = RaycastParams.new()
	RaycastParamaters.FilterDescendantsInstances(self.Character)
	RaycastParamaters.FilterType = Enum.RaycastFilterType.Blacklist
	
	local Results = workspace:Raycast(Origin, Direction, RaycastParamaters)
	
	if Results then
		print("Hit")
	else
		print("Missed")
	end
end


return ClientModule

client code:

local UIS = game:GetService("UserInputService")

local Module = require(script.Parent:WaitForChild("ModuleScript"))

Module.new()

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local character = player.Character

Module:Init(character)

UIS.InputBegan:Connect(function(key, gpe)

if key.UserInputType == Enum.UserInputType.MouseButton1 then

Module:Cast(workspace.CurrentCamera.CFrame.Position, mouse.Hit.p)

end

end)

The two problems I’m having is I’m not sure if it works (no errors in output) and I don’t know how to return the raycast result/how to use the result. I plan on using this to damage players. Sorry if my code is messy, if you have any tips for keeping code cleaner please let me know.

Try using the direction for the raycast as

direction = mouse.Hit.P - workspace.CurrentCamera.CFrame.Position

And on using the result of the raycast you can do

return Results

at the end of the cast function

This will return the RaycastResult of the raycast. To get what the raycast hit you can do Results.Instance

Okay, I added

local Direction = (Goal - Origin)

to the module

How would I fetch this data from the module?

wait nvm I would do
local results = Module:Cast(workspace.CurrentCamera.CFrame.Position, mouse.Hit.p)

lcoal raycastResult = ClientModule:Cast(Start, End)

also im not particular sure abot the application of OOP here since you are not inheriting from any parent modules and i assume there will only be one instance of this object per client. With something like this i typically just have an init() and deinit() function in the module.

1 Like

Could you go into more detail about what you mean? I tried doing

local results = Module:Cast(workspace.CurrentCamera.CFrame.Position, mouse.Hit.p)

print(results)

in the local script but, it didn’t print anything. Also, I’m new to OOP and don’t really understand when to and not use it.

function ClientModule:Cast(Start, End)
	local Origin = Start
	local Direction = End
	
	local RaycastParamaters = RaycastParams.new()
	RaycastParamaters.FilterDescendantsInstances(self.Character)
	RaycastParamaters.FilterType = Enum.RaycastFilterType.Blacklist
	
	local Results = workspace:Raycast(Origin, Direction, RaycastParamaters)
	
	if Results then
		print("Hit")
	else
		print("Missed")
	end
    return Results
end

You want to return the results from function

would you mind pasting the code you are using in the local script to interact with the module?

It already works, thanks! But, for some reason it wont filter the character.

I decided to ditch the argument I was sending from the local script to the module and instead define it in the module (I don’t know if this is good practice).

I put this in the module:

local Character = game.Players.LocalPlayer.Character

But, when I try and filter it:

RaycastParamaters.FilterDescendantsInstances(Character)

I get this error:

Players.dorious.PlayerScripts.ModuleScript:21: attempt to call a table value

RaycastParamaters.FilterDescendantsInstances = {Character, otherFitlers}
FilterDescendantsInstances takes a table, that is why u got an error for calling a table value.

RaycastParamaters.FilterDescendantsInstances = {Character}

trying setting it to just that.
Also there is nothing wrong with defining the character in the module, just make sure to redefine it every time the player spawns. I do this by defining the character in my init function and calling it each time the player spawns.

It kind of works but in this video you can see the output is inconsistent it only works when I get really close and click on the spawn even though I click on the baseplate multiple times.

Actually, I just placed some parts and it seems like it’s just the baseplate that is weird. Interesting.

Wait it’s actually still inconsistent.

Could this be because I’m firing this from the camera?

O that is something i didn’t think about, when the camera is zoomed about its not actually on the player so it will be inconsistent. Trying setting the origion to something like the players head or their HumanoidRootPart.

I set the Origin to the head but it’s still inconsistent.

maybe I should visualize the ray to see what it is doing.

Okay I visualized it using the camera but it doesn’t make any sense.

Last shot is apparently a miss?

I tried to replicate this code and i had the same random misses. To compensate for this i increased the magnitude of the cast by one and it seemed to fix the random misses.

local Direction = End-Start
Direction = Direction.Unit * (Direction.Magnitude + 1)

I did it by adding this code

1 Like

Thanks for helping me! This was way to complicated for no reason. Sorry for the hassle!

1 Like