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

How can I get player from character?

When the character touches a brick a GUI becomes visible.

I tried the GetPlayerFromCharacter method but the output is 22:05:01.018 - GetPlayerFromCharacter is not a valid member of Model 22:05:01.019 - Script 'Workspace.Part.Script', Line 4 22:05:01.021 - Stack End**

Here is the code

function onTouch(brick)
local p = brick.Parent:findFirstChild("Humanoid")
if p ~= nil then
p.Parent:GetPlayerFromCharacter()
end
end
script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

The GetPlayerFromCharacter funtion is a function of game.Players and uses the character you're wanting to get the player from as it's arguments!

function onTouch(brick)
	local p = brick.Parent:FindFirstChild("Humanoid")
	if p ~= nil then
		local plr = game.Players:GetPlayerFromCharacter(brick.Parent)
	end
end

script.Parent.Touched:connect(onTouch)

EDIT

If you need to get the PlayerGui of the player, the PlayerGui is a child of the Player.. so just index it like `plr.PlayerGui``

3 comments

0

It won't allow me to access things like PlayerGui. How would I do this?

0

If you got player using the above script, then playergui would be a child of the player for example print(player.PlayerGui.Name) would just print the name of playerGui, which is "PlayaerGui"

0

Edited

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