How to get player from character module?

  1. What do you want to achieve?
    I want to get the player from the character when you touch a part.

  2. What is the issue?
    I cant figure out how to do this

  3. What solutions have you tried so far?
    Here is what tried.

script.Parent.Touched:Connect(function(touched)
		if touched.Player.leaderstats.money.Value >= 0 then
			game.ServerStorage.orgdropper.Parent = game.Workspace
		end

	end)

I’m getting the “Player is not a valid member of the character” message.
Any help appreciated!

Player isn’t a child of character, its actually the other way around.

Thankfully Roblox has incorporated a method that returns the player for you.

:GetPlayerFromCharacter()

Here is the API: link

You can create a variable that represents the player to make it easier.

local player = touched.Parent:GetPlayerFromCharacter() 
--[[ you need the .Parent because the 'touched' 
represents a body part from the character, not the 
character model itself
--]]

if player.leaderstats.money.Value >= 0 then
    game.ServerStorage.orgdropper.Parent = game.Workspace
end
2 Likes

Thank you, dually noted!!

I tried to use this function, but failed in the 1st place

1 Like

i keep getting this error, how to fix?

This should work:

script.Parent.Touched:Connect(function(touched)
	local player = game:GetService("Players"):GetPlayerFromCharacter(touched.Parent)
	if player then
		if player.leaderstats.money.Value >= 0 then
			game.ServerStorage.orgdropper.Parent = game.Workspace
		end
	end
end)

actually it is game.Players:GetPlayerFromCharacter(Workspace.DevLazl)

yeah, I actually ended up figuring it out, but this works too.