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

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

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)

1 comment

0

How is that constructive in any way shape or form?

1 answer

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

11 comments

0

true, but can server scripts even access PlayerGui?

0

no You need to use this....... for i,v in pairs(game.Players:GetPlayers()) do and then say v.PlayerGui to access it

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)

0

oh! you were responding to my comment that I deleted! oops

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

0

guess I shouldn't delete comments even if they are wrong

0

You don't need the for loop.

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.

0

Server Scripts can access PlayerGui but cannot view its contents or edit anything

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.

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

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