Help making Gui visible to only the owner

I’ve been trying to make a gui that is only visible to the owner, my knowledge on scripting is kinda lackluster.
i’ve tried out multiple iterations of code and when i visited the place with my alt account, gui was still visible
I must be doing something wrong, any help is appreciated
Code so far for reference:

local button = script.Parent
local players = game:GetService("Players")
local owner = players.PlayerAdded = ("realknife")





local function findOwner()
	if not owner then
		button:Destroy()
	else 
		return
	end 
end

3 Likes

You can use a local script and adjust the code to make it visible to the owner like this:

local owner = "realknife"

if owner then
Gui.Visible = true
else
Gui.Visible = false
end
1 Like

Sadly this did not work, it still shows up when i join with my alt
and yes, i added this:

local Gui = script.Parent

Added script in StarterCharacterScripts local script

And type local Gui = game.StarterGui – your gui

local button = game.StarterGui --- your gui 
local players = game.Players.LocalPlayer
local owner = players.PlayerAdded = ("realknife")




local function findOwner()
	if not owner then
		button.Visible = true
	else 
button:Destroy()
		return
	end 
end
1 Like

Hey there,

You’ve got a couple errors in your code here

  1. You are setting a connection to a string, which doesn’t make sense
  1. This function was never called, though I’m sure you would have in the end

I do understand you’re a bit new to coding though, so I can help you out a bit.

The best way to detect if the gui is in the game owner’s PlayerGui is to use the UserId. This is because the owner (you) could change their name to like, I don’t know “realspoon”, and the script would hence detect that your name doesn’t match “realknife”, which would return false.

You can use game.CreatorId to get the game owner’s UserId.

local Players = game:GetService("Players")
local player = Players.LocalPlayer -- THIS IS YOU

local button = script.Parent
if player.UserId ~= game.CreatorId then -- If your UserId isn't the game creator's UserId
    button:Destroy()
end

Make sure that you are putting this in a local script that is inside the button :slight_smile:

2 Likes

That seemed to solve it, when i join with my alt, it’s no longer visible, i’ll mark it as a solution

2 Likes

This will not function.

game.StarterGui or game:GetService("StarterGui") is a service replicates the pre-set UI to players who join the game. Editing any of its elements will not replicate to the client.

The way to declare the player’s gui is simply to wait for the PlayerGui of the player instance, which can be done by saying

local player = game:GetService("Players").LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")

Scripts that handle GUI functionality will rarely appear in StarterCharacterScripts. It is good practice to handle them in StarterPlayerScripts, since ScreenGuis are holders for UI elements.

1 Like

Little later on, i began to wonder, what to do if let’s say i want GUI to display for not only myself, but also a select few users?

You can have a list of UserIds that you want this to apply to and use table.find(array, value) to help you check if your player userId matches any of the ones in the list.

local list = {
    game.CreatorId, -- YOU
    1353251234,
    1,
    69420,
}

local Players = game:GetService("Players")
local player = Players.LocalPlayer -- THIS IS YOU

local button = script.Parent
if not table.find(list, player.UserId) then -- Can't find your userId
    button:Destroy()
end
1 Like

Will do, thanks for a quick reply.

1 Like