Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make this Ray ignore hats?

Asked by 6 years ago

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?

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

0
I'm not sure I follow what the "i" is supposed to stand for in **targetHats[i] = child**. I apologize TheOddyssey 9 — 6y
0
That's my mistake - should have proofread the answer before posting. Changed it now - sorry! LukeSmasher 619 — 6y
0
Hm, it seems my main problem now is somehow making 'targetCharacter' all of the Player Character's in Workspace whilst this is in a Script not a LocalScript. TheOddyssey 9 — 6y
Ad
Log in to vote
0
Answered by
angeI1001 123
6 years ago

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.

Answer this question