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

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

Asked by 6 years ago
Edited 6 years ago

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)
0
connect is deprecated. Use Connect instead. green271 635 — 6y

3 answers

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

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)
0
This is incorrect too. Avigant 2374 — 6y
0
connect is deprecated. Use Connect instead. green271 635 — 6y
0
Fixed my answer. MythicalShade 420 — 6y
0
Thank you! :D feferoblox 12 — 6y
0
Glad to help MythicalShade 420 — 6y
Ad
Log in to vote
2
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago

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.

0
I've never had any problem with mine or Ap's, so how would it be incorrect? MythicalShade 420 — 6y
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. Avigant 2374 — 6y
0
Okay thanks for explaining MythicalShade 420 — 6y
0
Ah, I'll use this then. Ap_inity 112 — 6y
Log in to vote
1
Answered by
Ap_inity 112
6 years ago
Edited 6 years ago

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)
0
This is incorrect. Avigant 2374 — 6y
0
connect is deprecated. Use Connect instead. green271 635 — 6y
0
I forget sometimes ;3; I fixed my answer. Ap_inity 112 — 6y

Answer this question