Answered by
2 years ago Edited 2 years ago
Okay, I found the problem. You see, in the Roblox Creator Documentation, it says that Workspace:GetPartsInPart()
returns an array (table in which the "key" is and should be a number and therefore called "index" and its "value" is any
; e.g., {"hi", 2, 5, "six"}
or {[1] = "hi", [2] = 2, [3] = 5, [4] = "six"}
) of BasePart
s, but it didn't specify if it returns nil when there are no BasePart
s. That could be why it still damages your character. Workspace:GetPartsInPart()
returns an empty table when mid-air, and your script checks if it exists (not nil
), and an empty table DOES EXIST.
Solution
To fix this, you will check if it's an empty table by getting the number of elements stored in it using the #
operator. You can also use table.getn()
if you want to be nostalgic or keep the legacy, but it's EXTREMELY DEPRECATED and the #
operator is much more efficient and faster and easy to use.
Also you don't need to get every limb of the player and define it in a variable, you can just iterate every child of the character and check if it's a BasePart
using Instance:IsA()
.
Also checking if script is still a descendant of StarterPlayer
is EXTREMELY UNNECESSARY since the scripts inside it won't run until it is moved in either PlayerScripts
inside the Player
, or inside Player.Character
.
Tip: You can replace RunService.Heartbeat
with task.wait()
since it does the same thing. To loop it, use a while true do
loop.
Final Script
01 | local Character = script.Parent |
02 | local Humanoid = Character:FindFirstChildOfClass( "Humanoid" ) |
05 | local Params = OverlapParams.new() |
06 | Params.FilterType = Enum.RaycastFilterType.Exclude |
07 | Params.FilterDescendantsInstances = { Character } |
09 | for _, Child in ipairs (Character:GetChildren()) do |
11 | if Child:IsA( "BasePart" ) then |
12 | local Parts: { BasePart } |
13 | if (Child.Name = = "Head" ) or (Child.Name = = "HumanoidRootPart" ) then |
14 | Parts = workspace:GetPartsInPart(Child, Params) |
16 | Parts = workspace:GetPartsInPart(Child) |
19 | if Parts ~ = nil then and Character.Head.AssemblyLinearVelocity.Magnitude > 75 then |
21 | Humanoid:TakeDamage( 1 ) |
22 | print (Child.Name, Parts) |