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.
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.
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
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
RaycastParamaters.FilterDescendantsInstances = {Character, otherFitlers}
FilterDescendantsInstances takes a table, that is why u got an error for calling a table value.
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.
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 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)