Error with PlayerGui in localscript

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

So i have a button inside of a textlabel, the textlabel’s text is a player’s name(this player is also in the game)
When you click this button i want to fire a bindable event inside of the player’s PlayerGui that i want to trade with.

  1. What is the issue? Include screenshots / videos if possible!

The problem is that it does find the player, but it indexes nil with PlayerGui! Btw PlayerGui already loaded so far and it’s done from a localscript

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I couldn’t find any solutions here or anywhere else, that’s why i need help!

Here’s what the script looks like :

script.Parent.MouseButton1Click:Connect(function()
local player1 = game.Players:FindFirstChild(script.Parent.Parent.Text)
local player2 = script.Parent.Parent.Parent.Parent.Parent
local event = player1.PlayerGui.Trade.TradeRequest – Error here
event:Fire(player1.Name, player2.Name)
end)

2 Likes

Also how do u write in lua on a phone?

Copy and paste this: ```

Then do the rest if you want to write lua on phone.

1 Like

Make sure your variables are referenced correctly. For readability sakes and organisation, you should probably reference your player without that much .Parent

I would try chucking a WaitForChild on the PlayerGui, as well as debugging the code, e.g printing the player name, checking to see if PlayerGui is not equal to nil, etc.

1 Like

found problem:

local player2 = script.Parent.Parent.Parent.Parent.Parent

replace it with

local player2 = game.Players.LocalPlayer
1 Like

If its a local script you can use

game.Players.Localplayer

to go get the player.

1 Like

The mouse button click also refs the player who clicked so you could use:

script.Parent.MouseButton1Click:Connect(function(Plr)
local player2 = Plr
1 Like

So as I understand it player1 has a thing on their screen that they click which causes a thing to pop up on player2 screen?

At the moment you’re trying to directly reference a gui object of another player’s player gui which you just can’t do.

If you want to trigger something happening on another player’s screen then you’ll need to use remotes. Player1 fires a remote that tells the server to notify player2, the server then fires a remote to notify player2 about what’s going on. Player2 then handles the pop up themselves.

1 Like

I think this is for surface guis that are in the workspace rather than player gui?

1 Like

No it’s in PlayerGui inside of a player

There is a bindable event inside of player2’s PlayerGui, i want to fire that event but it indexs nil with playergui, not the event

Yeah so this:

script.Parent.MouseButton1Click:Connect(function(Plr)
local player2 = Plr

wouldn’t work as your guis are in playergui.

1 Like

I think you haven’t understood what I’ve said. I’ll go through the code and comment where things will break because of the server-client boundary / replication restrictions.

script.Parent.MouseButton1Click:Connect(function()
local player1 = game.Players:FindFirstChild(script.Parent.Parent.Text) -- Player 1 is a different player than the one running this script
local player2 = script.Parent.Parent.Parent.Parent.Parent  -- Player 2 is the local player
local event = player1.PlayerGui.Trade.TradeRequest -- Player 1's playerGui is not visible to player 2 (who is the one running this script) so anything within player1's player gui is not replicated to player 2
event:Fire(player1.Name, player2.Name)
end)
1 Like

Is there any other way i can use bindable events outside of the playergui for this? or does it have to be a remote event? I’m making a trading system and it requires aclot of client-to-client communication.

Is there even a trading system tutorial in general? If there is that would be really helpful, because then I know how they made client-to-client communication without 20 remote events just for simply showing a frame and some pets inside of it.

Any client to client communication needs to go through the server somehow and the way that is done in scripts like this is with remotes. You don’t need a lot of remotes to make this work as you can make use of the arguements that you can pass when you fire / invoke a remote.

I imagine there’s probably a tutorial around somehow on how to do it but I can’t remember seeing any that I can link here right now unfortunately.

1 Like

You could even do it with 1 event if you have a lot of if statements but there isn’t a reason to.