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

PlayerGui is not a valid member of player?

Asked by 4 years ago

I've had to ask this many times so I am going to be as detailed as possible. I'm making a sell to player gui and this is under the StarterPlayerScripts:

local Player = game.Players.LocalPlayer -- Getting the player   
local Mouse = Player:GetMouse() -- Getting the players mouse


Mouse.Button1Down:Connect(function() -- Detecting when player presses the left mouse button

    if Mouse.Target.Parent:FindFirstChild("Humanoid") and 
        Player.TeamColor.Name == ("Dirt brown") then -- Checking if the Mouse is over a player and on a selling team
        print(Mouse.Target.Parent.Name) --Check  other player's name
    local mousetarget = Mouse.Target.Parent
local target = game:GetService("Players")[mousetarget.Name]
        target.PlayerGui.StartScreen.Frame.Visible = true --Open other player's Gui
        print("Debug") --Debug

    end
end)

When someone clicks another player a gui pops up on the other players screen. I've been told to do remote events and more but this is the closest i have gotten. When clicking on another player I am given:

PlayerGui is not a valid member of player --Line 12

1 answer

Log in to vote
1
Answered by
rexhawk 222 Moderation Voter
4 years ago

The answer for this is Remote Events

Remote events are like small doors to the server to execute specific functions that will replicate across all the clients (otherwise, changes you make on your client will STAY on your client)

To fix this (in other words, what I'd do), you'd fire a Remote event to the server telling the server that the player clicked another player. The server would receive this and tell the player that you clicked to display the GUI.

First, you'd need to put a remote event in ReplicatedStorage called "PlayerClickedPlayer" and put this script in ServerScriptService:

--// Services
local replicatedStorage = game:GetService("ReplicatedStorage")

replicatedStorage:WaitForChild("PlayerClickedPlayer").OnServerEvent:Connect(function(player,subject)
    game:GetService("ReplicatedStorage"):WaitForChild("PlayerClickedPlayer"):FireClient(subject)
end)

And this is your fixed code that you posted before (client script):

--// Services
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
--// Objects
local player = players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
    local player = players:GetPlayerFromCharacter(Mouse.Target.Parent)
    if player and player.TeamColor.Name == ("Dirt brown") then
        replicatedStorage:WaitForChild("PlayerClickedPlayer"):FireServer(player)
    end
end)

replicatedStorage:WaitForChild("PlayerClickedPlayer").OnClientEvent:Connect(function()
    player:WaitForChild("PlayerGui").StartScreen.Frame.Visible = true
end)

(Also improved some things for you)

Hope this helps

0
I can't get the server to connect Darksniper600 104 — 4y
Ad

Answer this question