The title says it all.
I need to know how would I tell if the game owner is present in-game using a server script.
I thought of using FindFirstChild but couldn’t find a way to get the name from id.
Any help appreciated.
Thanks.
game.Players.PlayerAdded:Connect(function(Player)
if Player.UserId == 0 then
print(“OWNER IS IN THE GAME!!”)
end
end)
Change the 0 to the owner id if the game is in a group.
If not just use game.CreatorId
You could check to see if the UserId matches game.CreatorId. For example:
game:GetService("Players").PlayerAdded:Connect(function(plr)
if plr.UserId == game.CreatorId then
print("Owner is in game!")
end
end)
function onPlayerAdded(plr)
if game.CreatorType == Enum.CreatorType.Group and plr:GetRankInGroup(game.CreatorId) = 255 then
--Does the game belong to a group? If so, check if the player is rank 255 (owner) in it
elseif game.CreatorType == Enum.CreatorType.User and plr.UserId == game.CreatorId then
--Otherwise, if the game belongs to a user check if the new player's UserId matches the game's CreatorId
end
end
No need to worry about the owner’s name, OP. All that matters here is their UserId and the game’s CreatorId.
However, if you ever do need to get a player’s username from a UserId, there’s always Players:GetNameFromUserIdAsync(UserId)
game.Players.PlayerAdded:Connect(function(Player)
if Player.UserId = game.CreatorId then
-- Code here!
end)
This only detects once the owner joins.
I need something that would detect if the owner already have joined.
As I said beforehand, this only detects once the owner joins.
local Players = game:GetService(“Players”)
local found = false — will be used to determine if the creator is found
— Loop through all connected players. We don’t need index, so it’s named _
for _, currentPlayer in pairs(Players:GetPlayers()) do
— This player is the creator!
if currentPlayer.UserId == game.CreatorId then
found = true
break — We don’t need to continue loop after finding creator
end
end
— Was the creator found during the loop?
if found then
— Code here!
end
Sorry, I’m not sure how to put double hyphen for comments, so they are single hyphen.
My example code is universal. As an alternative to detecting the owner when they join, you could put the if statement in a for _,player in ipairs(Players:GetPlayers()) do
loop. Alternatively, when the owner joins you can set a BoolValue
in ServerStorage or ReplicatedStorage to true, and set it to false when the owner leaves - that way you’re not looping through the player list whenever you need to see if the owner is ingame.