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)
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