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
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
You are setting a connection to a string, which doesn’t make sense
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
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.
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