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)
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)
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
.
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)