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

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

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

2 comments

1

It seems you've got a bit confused. Change line 4 to this; local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

0

That worked^

1 answer

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)

1 comment

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.

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