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

Why is my GetOwnerName() function not working?

I am trying to make an admin command script, and I have made a function that is supposed to get the owners name, which(I have not added this yet) will then insert the name into the Admins list.

I do not know why it is not working, and I am in need of help, here is the script.

local owner = game.CreatorId
local admins = {"NameHere","AnotherAdmin"}

-- Funcs
game.Players.PlayerAdded:connect(function(plr)
function GetOwnerName()
	if plr.UserId == owner then
		local x = plr.Name
	end
end
local ownername = GetOwnerName()
local m = Instance.new("Message", workspace)
m.Text = ( ownername.. " has Joined the server")

end)

Any and all help is appreciated and needed.

2 answers

✓ Accepted answer

This is not a good way to give admin commands ect as players can change their name, so I would use their id e.g plr.UserId

The main problem here is that you do not return the value from the function so nil will be present which you cannot concatenate a string with.

local owner = game.CreatorId
local admins = {"NameHere","AnotherAdmin"}


game.Players.PlayerAdded:connect(function(plr)
	
	-- so this local function needs the player now that ist local
	local function GetOwnerName(plr)
	    if plr.UserId == owner then -- check if it is the owner
	        return plr.Name -- the problem is that you dont return the info
	    end
	
		return false -- not the owner so give false back
	end

	local m = Instance.new("Message", workspace)
	local plrName = GetOwnerName(plr)
	
	if plrName then
		m.Text =  plrName .. " is the owner"
	else 
		m.Text =  plr.Name .. " is not the owner"
	end
	
end)

My Recommendations

As I said at the top please use the player id as it will not change.

I think that you will want a level bases system as you have owner and admins so I would use a table e.g.

local adminList = {
	[use id here] = { permission level, gear ect},
	-- other admins
}

This will allow you to change or add who gets what.

2 comments

0

You should add that you can use "if adminList[plr.UserId] then print('isAdmin') end", no need to loop trough the table

0

I had been using the UserId of the game owner, as I was planning on making the Admin list into UserID, I just had not developed an admin commands EVER, so I was going to do a simple ideal first, then edit and keep editing as it went along.

If you want a function to return something, use return VALUE1,VALUE2,.... In your example, use return plr.Name instead of local x = plr.Name.

Also, if there is no owner online, GetOwnerName() will (also) return nil, as it does now. Under local ownername = GetOwnerName() add this:

if not ownername then return end
-- Code below here will only be used is the player is an owner

2 comments

0

The message part of this script still does not work, it doesn't even put a message in-game(im using in studio)

0

In Play Solo, the player is already there before scripts run. To test this stuff, use Start Server and Start Player

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