Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is this the correct way to use :GetTouchingParts()?

What is my goal?

I am trying to have a touch functions that constantly updates whether the right leg or left leg is touching the ball.

I assume :GetTouchingParts() is the right method to accomplish this however the variables keep getting set to false and is never true even when i hit the ball with my leg.

function touchFunction(hit)
	if hit.Name == "Ball" then
		
		local t = hit:GetTouchingParts()
		
		if table.find(t, 'Right Leg') then
			rightLegIsTouching = true
		else
			rightLegIsTouching = false
		end
		
		
		if table.find(t, 'Left Leg') then
			leftLegIsTouching = true
		else
			leftLegIsTouching = false
		end
		
	end
end

Player.Character[currentFoot].Touched:connect(touchFunction)

Any help is appreciated.

Merry christmas to you all.

1 comment

0

LeftLeg and RightLeg do not have spaces in their name and also you can't use string in table.find in this case because there is no strings in this table, only Instances

1 answer

✓ Accepted answer

GetTouchingParts returns Instances

GetTouchingParts returns table of Instances touching given part, it does not returns string so table.find in your case can't be used.

ipairs loop

You can create simple similar function tho using pairs, in this case ipairs to prevent not very rare errors if player leaves the game during loop:

        local t = hit:GetTouchingParts()

	local function Find(Name)
		for _, Part in ipairs(t) do
			if (Part.Name == Name) then
				return Part
			end
		end
	end

        if Find('RightLeg') then
	...

The way pairs works is that it returns 2 values, Index - Position or key of the value in the table and Value - value of the index in the table. ipairs is the same but it breaks if index is nil or is not number so if player leaves and the Part variable will be nil, it won't loop but will break the loop.

Sources

GetTouchingParts

ipairs/pairs

This discussion is preserved as it was. New answers, comments, and votes are closed.