Hey! Quick question! How would I get the player from the character when they touch a brick so I can access a gui inside a players player gui?
You need to find hit.Parent's name and use findFirstChild in Players to find the player. You can then access PlayerGui.
01 | debounce = false |
02 | local sp = script.Parent |
03 | function T(hit) |
04 | debounce = true |
05 | local char = hit.Parent |
06 | local players = game:GetService( "Players" ) |
07 | local player = players:findFirstChild(char.Name) |
08 |
09 | local gui = player.PlayerGui.(GUINAMEHERE) |
10 | wait( 1 ) |
11 | debounce = false |
12 | --do whatever here |
13 | end |
You're welcome.
You can use Parameters to access the Player while using a touch event. I post a example on How to use Parameters below:
1 | script.Parent.Touched:connect( function (Player) |
2 | print (Player.Parent.Name) --This prints out the name of the Player who "activated" the script. |
3 | game.Players [ Player.Parent.Name ] .PlayerGui -- Add the Gui Path here. |
4 | end ) |
Here more information about Parameters
You can also use GetPlayerFromCharacter()
to get the Player from the Character. Here's a example on how to use it.
1 | script.Parent.Touched:connect( function (Player) |
2 | local Character = game.Workspace.Player |
3 | local plr = game.Players:GetPlayerFromCharacter(Character) |
4 | print (plr.Name) |
5 | plr.PlayerGui -- Put the Path of the Gui here |
6 | end ) |
If you want information about GetPlayerFromCharacter()
I use this script myself works everytime.
01 | local gui = script [ "put gui inside of script" ] |
02 | script.Parent.Touched:connect( function (hit) |
03 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
04 | if plr and not plr.PlayerGui:findFirstChild(gui.Name) then |
05 | local cl = gui:Clone() |
06 | cl.Parent = plr.PlayerGui |
07 | coroutine.resume(coroutine.create( function () |
08 |
09 | local human = hit.Parent:FindFirstChild( "Humanoid" ) |
10 | --Basically this just checks to see if it is a real player touching this brick. |
11 | if (human ~ = nil ) then --If it is a real player, then DESTROY THEM! |
12 | human.Health = 0 --Your Health Is Now 0. |
13 | end |
14 | wait( 5 ) |
15 | for i = 0 , 1 ,. 1 do |