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

How to get a player from hitting a part? [Solved]

Asked by 3 years ago
Edited by JesseSong 3 years ago

Took a long break from coding and im getting back into it but forgot how to get a player from the character when the character hit a part. Its so when the player hits the part they get 5 coins.

Code

local coin = game.Workspace.coin
coin.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then 
        local player = hit:GetPlayerFromCharacter(hit.Parent)
        player.leaderstats.Coins.value = player.leaderstats.Coins.value + 5
    end
end)

Error message 23:35:27.588 GetPlayerFromCharacter is not a valid member of MeshPart "Workspace.bestshot123.LeftHand" - Server - Script:4

1
It seems you've got a bit confused. Change line 4 to this; local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) MarkedTomato 810 — 3y
0
That worked^ bestshot123 38 — 3y

1 answer

Log in to vote
0
Answered by
Jac_00b 157
3 years ago

On line 4 you put just "hit" before GetPlayerFromCharacter. This means that it will go to the body part, which in this case was the left hand. Change it to hit.Parent, because the parent of the left hand has the actual character. So your new line 4 would be

local player = hit.Parent:GetPlayerFromCharacter(hit.Parent)
0
Your answer is not correct, but the code is correct. Basically the error indicates the object parameter (hit) is being referenced with  a meshpart (GetPlayerFromCharacter). Hit is not a method or a function. It's just a datatype. JesseSong 3916 — 3y
Ad

Answer this question