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

How do I GetPlayerFromCharacter on a Table?

Asked by
Borrahh 265 Moderation Voter
3 years ago

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

Log in to vote
1
Answered by 3 years ago

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)

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. Alexander_Ashford 231 — 3y
Ad

Answer this question