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

Verify if it's a player touching? ( SOLVED )

So, I'm trying to make a script that if a part is touched, it does something, but, it has to be a player touching the part... How to verify if a player is touching the part?

script.Parent.Touched:connect(function(playerlol)
	playerlol.Parent.Humanoid.WalkSpeed = 100
	playerlol.Parent.Humanoid.JumpPower = 80
	game.Workspace.Gravity = 100
	game.Workspace.Song:Play()
end)

1 comment

0

connect is deprecated. Use Connect instead.

3 answers

✓ Accepted answer

One way is to just check if what hit has a humanoid. The other way is to see if the name of what hit is a player, I combined them both here.

script.Parent.Touched:Connect(function(playerlol)
	if playerlol.Parent:FindFirstChild("Humanoid") and game.Players[playerlol.Parent.Name] then
		print"is a player"
    	playerlol.Parent.Humanoid.WalkSpeed = 100
   	 	playerlol.Parent.Humanoid.JumpPower = 80
   	 	game.Workspace.Gravity = 100
   	 	game.Workspace.Song:Play()
	end
end)

However, like Avigant said, playerlol.Parent could be nil, so to account for this there's an other way to test if it's a character. This is by using GetPlayerFromCharacter() which returns the player associated with the given character.

To use it, write the following code.

script.Parent.Touched:Connect(function(playerlol)
	if playerlol.Parent and game:GetService("Players"):GetPlayerFromCharacter(playerlol.Parent) then
		print"is a player"
    	playerlol.Parent.Humanoid.WalkSpeed = 100
   	 	playerlol.Parent.Humanoid.JumpPower = 80
   	 	game.Workspace.Gravity = 100
   	 	game.Workspace.Song:Play()
	end
end)

5 comments

0

This is incorrect too.

0

connect is deprecated. Use Connect instead.

0

Fixed my answer.

0

Thank you! :D

0

Glad to help

You'd use the game.Players:GetPlayerFromCharacter() method to test.

BasePart.Touched:Connect(function(TouchedPart)
	if not game.Players:GetPlayerFromCharacter(TouchedPart.Parent) then
		return
	end

	-- It's a player!
end)

Remember that TouchedPart.Parent can always be nil.

4 comments

0

I've never had any problem with mine or Ap's, so how would it be incorrect?

0

Because as I said in my answer, the touching part's parent can be nil, which you are not accounting for, and non-player objects can also have objects named "Humanoid" as a child.

0

Okay thanks for explaining

0

Ah, I'll use this then.

You can check if a player is touching by adding if playerlol.Parent:FindFirstChild("Humanoid")

I've fixed your script, it should look like this:

script.Parent.Touched:Connect(function(playerlol)
if playerlol.Parent:FindFirstChild("Humanoid") then
		playerlol.Parent.Humanoid.WalkSpeed = 100
		playerlol.Parent.Humanoid.JumpPower = 80
		game.Workspace.Gravity = 100
		game.Workspace.Song:Play()
	end
end)

3 comments

0

This is incorrect.

0

connect is deprecated. Use Connect instead.

0

I forget sometimes ;3; I fixed my answer.

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