I have a line of code here that fires off a ray along with a Blacklist. However, the ray does not ignore hats. Whenever a player shoots a player's hat, the ray stops at the hats and the player takes no damage as if he were wearing a shield.
Caster:FireWithBlacklist(FirePointObject.WorldPosition, Direction * Module.Range, Module.BulletSpeed, {Handle, Tool.Parent, game.Workspace:FindFirstChild("Ignore")}, Bullet)
I have done quite some extensive research on this in hopes to be able to fix it. However, I have come up empty handed every time. I normally get errors like:
Unable to cast value to Object
or some other ones that I forget.
At some point, I tried to do:
Caster:FireWithBlacklist(FirePointObject.WorldPosition, Direction * Module.Range, Module.BulletSpeed, {Handle, Tool.Parent, game.Workspace:FindFirstChild("Accoutrement") game.Workspace:FindFirstChild("Ignore")}, Bullet)
But then that would make the ray go through all of the character parts not just the hats and it does not do damage.
How would I fix this to where the ray is able to hit the character's head, even if the character is wearing a mask or so?
There is a very helpful variant of the function :FindPartOnRay()
which can be used for this purpose, which is :FindPartOnRayWithIgnoreList()
.
Essentially this function takes a table as its second parameter in which you can store all the parts you want the ray to ignore, and it will.
In your case, you'd want to get all the player's hats by iterating through their character and using the IsA()
function to test their class type.
local targetHats = {} for i, child in pairs(targetCharacter:GetChildren()) do -- Iterate through target player's children if child:IsA("Accessory") then -- If the child is a hat (accessory) targetHats[i] = child -- Append the child to the array end end
The table targetHats
is the table you can plug into the second parameter of your FindPartOnRayWithIgnoreList()
function.
For more information, take a read of Roblox Wiki's page.
Here's an example of how you could do that: https://scriptinghelpers.org/questions/49276/how-do-i-ignore-multiple-parts-with-raycasting
I hope this helped you out.