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

'Not a valid member of PlayerGui' when it is there?

Asked by 5 years ago

I am making a script which, when a player touches a massive invisible 'zone block', it will change the zone name at the top of the screen in their UI. I've hit many hurdles with this, but my current one is just perplexing me. When touching the block this script is contained in, I recceive the error "TownName is not a valid member of PlayerGui", which simply isn't true, it is very much a child of PlayerGui at the time of the error. I've tried a few fixes such as a WaitForChild("TownName") function which seemed to give no output whatsoever and I really do not know what to try next.

local debounce = true


function onTouched(hit)

    local player = hit.Parent:findFirstChild("Humanoid")
    local char = game.Players:GetPlayerFromCharacter(hit.Parent)
    local tngui = char.PlayerGui.TownName.Background.Text
    local name = script.Value.Value

    if player == nil then return end
    if tngui.Text == name then return end

    if debounce == true then
    debounce = false
    tngui.Text = name

    wait(2)
    debounce = true

    end
    end

script.Parent.Touched:connect(onTouched)
0
How is that constructive in any way shape or form? mazejaker -6 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

PlayerGui is located in Players, not the Character. If this is a server script do

game.Players.PlayerAdded:Connect(function(Player)
    local PlayerGui = Player.PlayerGui
end
0
true, but can server scripts even access PlayerGui? OBenjOne 190 — 5y
0
no You need to use this....... for i,v in pairs(game.Players:GetPlayers()) do and then say v.PlayerGui to access it WillBe_Stoped 71 — 5y
0
yes, but you are still changing something Client side from the server, right? or can this function do that... (As I think of all the ways this might come in handy) OBenjOne 190 — 5y
0
oh! you were responding to my comment that I deleted! oops OBenjOne 190 — 5y
View all comments (7 more)
0
just to make this less confusing it was the second comment and said something like "use :GetPlayerFromCharacter()" but then I saw that that was already in the script OBenjOne 190 — 5y
0
guess I shouldn't delete comments even if they are wrong OBenjOne 190 — 5y
0
You don't need the for loop. User#21998 0 — 5y
0
I'm not 100% sure how this would help? The script is accessing the PlayerGui just fine, but it's not finding the 'TownName' ScreenGui that's in there. Or am I misunderstanding how this works? I'm almost certain I have done something very similar to this in the past. mazejaker -6 — 5y
0
Server Scripts can access PlayerGui but cannot view its contents or edit anything green271 635 — 5y
0
Is that so? That must be 'new' (in terms of when I last attempted to script anything). I assume the same goes for many any of the player's contents like the Backpack etc? I guess I will try Shinjaa's method then. Seems like an overcomplication to me. mazejaker -6 — 5y
0
Could I use a RemoteFunction in the part that detects the player to then call a script that is inside the player character? I genuinely have no idea how to use RemoteFunctions though lol mazejaker -6 — 5y
Ad

Answer this question