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

How do I trigger a script when a certain body part touches a part?

In my game, if the player steps on a certain pad they will be granted +10 coins. Here is the script I'm using:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then wait(0.4)
		game.Players:FindFirstChild(hit.Parent.Name).moneystats.Coins.Value += 10
		wait (5)
	end
end)

This script works, but it has an issue. If it registers both legs landing on the pad at once, (such as when a player jumps on to it), it will execute the script twice and award 20 coins. I tried to fix this by awarding the coins only if the left leg touched the pad. That way, the script will not be triggered twice if both legs touch at the same time. To do this, I used the above script but changed ("Humanoid") to ("LeftLeg"):

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("LeftLeg") then wait(0.4)
		game.Players:FindFirstChild(hit.Parent.Name).moneystats.Coins.Value += 10
		wait (5)
	end
end)

However, this script resulted in no coins being awarded. Does anyone know how to fix the script so the coins will be awarded only when the left leg touches the pad?

3 comments

0

Your script will probably work if the player's body type is r6. Already try it on r6?

0

Yes, I made the game for r6, and that is what I have been testing in.

0

Its because its not a ("LeftLeg") on the R6 but its ("Left Leg")

1 answer

That's because "LeftLeg" is not part of the Body but "Left Leg" is the correct name.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Left Leg") then wait(0.4)
        game.Players:FindFirstChild(hit.Parent.Name).moneystats.Coins.Value += 10
        wait (5)
    end
end)

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