Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is my GetOwnerName() function not working?

Asked by
IcyEvil 260 Moderation Voter
7 years ago

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

Log in to vote
0
Answered by 7 years ago

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.

0
You should add that you can use "if adminList[plr.UserId] then print('isAdmin') end", no need to loop trough the table einsteinK 145 — 7y
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. IcyEvil 260 — 7y
Ad
Log in to vote
0
Answered by
einsteinK 145
7 years ago
Edited 7 years ago

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
0
The message part of this script still does not work, it doesn't even put a message in-game(im using in studio) IcyEvil 260 — 7y
0
In Play Solo, the player is already there before scripts run. To test this stuff, use Start Server and Start Player einsteinK 145 — 7y

Answer this question