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

How do I GetPlayerFromCharacter on a Table?

Basically, RoomOneTable[1] == Workspace.EEnergy8 I'm trying to get the Player from Character Error: attempt to index nil with 'Name

local playerRoomOneOne = game.Players:GetPlayerFromCharacter(RoomOneTable[1])
print("Player one from the table, 1 is: "..playerRoomOneOne.Name)

1 answer

✓ Accepted answer

You are currently trying to get the player from the object instead of a character which is why it is not working. The thing you need to do is check if something touches the part. If it has a humanoid then the parent of it is a character. You can then GetPlayerFromCharacter and run the rest of your script.

I have attached a sample script below.

--VARIABLES:

--In this case you would want to reference the table instead ie game.Workspace.EEnergy8
local Part = game.Workspace.Part
local Players = game:GetService("Players")

--hit is the thing which touches the part (or in your case table)
Part.Touched:Connect(function(hit)
	
	--In order to get the player from character you first need to find a character.
	--All characters have humanoids so first check if whats touching the table has a humanoid.
	if hit.Parent:FindFirstChild("Humanoid") then
		
		--We know hits parent is the character because it has a first child of humanoid.
		--Now we can use Players:GetPlayerFromCharater
		local playerRoomOneOne = Players:GetPlayerFromCharacter(hit.Parent)
		
		print("Player one from the table, 1 is: "..playerRoomOneOne.Name)

	end
end)

1 comment

0

Additional Note: You are probably going to want to add a debounce so it doesn't print the player touching the part multiple times. Moreover if EEnergy8 is a model you are going to need to reference a specific part within the model that you want to detect players touching.

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