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

How to print Username based from UserID from my script? (not a basic question about UserID/Username)

Asked by 5 years ago
Edited 5 years ago

So, hey 'der!

I've found a script on roblox for free which helped me out. The script is used to ban people from my game. However, i'd like to get their Username too from their Userid (I ban them if their userid match from the bannedlist)

Example of the script below.

bannedplayerid = script.UserID.Value--the person to ban (userid e.g. 12345678)
banreason = script.BanReason.Value --reason for ban
anonymousbanner = false --if the banner is anonymous (e.g false)
banner = "Tom" --put whoever banned the person here
enforceban = true --if the ban is enforced (e.g true)
note = script.BanAppeal.Value --e.g. message someone to appeal
secretban = false --if when banned it displays the ban message (false) or "You have lost connection to the game" (true)
Alert = script.Parent.Alert -- Sound
Alert2 = script.Parent.Alert2  -- Sound
Username = script.Name -- Scripts name. 
BanStat = script.BanStatus.Value -- If user still is banned, or that i did unban. 

--------------------------------------------------------------------------------------------------------------------------------------------------------

game.Players.PlayerAdded:connect(function(instance)
    wait()
    if instance.UserId == bannedplayerid then
        wait()
        if not enforceban == false then
            wait()
            if secretban == false then  --Secretban
                wait()
                if BanStat == true then --BanStat
                    wait()
                    if Alert.Playing == false then
                        wait()
                        Alert:Play()
                        wait()
                    else
                    end
                        if anonymousbanner == true then
                            ( HERE IS THE BANLINE THAT KICKS PEOPLE )
                        else
                            ( HERE IS THE BANLINE THAT KICKS PEOPLE )
                        end
                else --BanStat
                end --BanStat
            else --Secretban
            end --Secretban
        end
    end
end)

At the line where it says "( HERE IS THE BANLINE THAT KICKS PEOPLE )" i'd like to place a line that would create a message like "Dear " ..Username.. ". You have been banned" -- And more text.

I would like that Username would be the username from ID and not the name from the script.

I've done some research through google and official roblox wiki thing. But i couldn't figure it out.

It would be nice if you guys could help me :-)

EDIT: Can i also add a line that would a badge to the player by playerid?

Thanks :-)

0
If this solves your problem, be sure to add [Solved] to the title of your question so that people know you don't need anymore help. User#25115 0 — 5y
0
Aaaaaaa why do you use wait() everywhere? Why do you use global variables? Why do you use lowercase :connect()? Why don't you merge multiple if statements into one? Your code needs a big boi rewrite xd Amiaa16 3227 — 5y

2 answers

Log in to vote
0
Answered by
sydre2 25
5 years ago

To get the name of a player from the UserId, You can use the built-in function, Players:GetPlayerByUserId(Player.UserId) which will return the name of the player with the UserId you passed into the brackets.

0
But, how? i don't get it tbh. There is no Players in the script. I'm sorry if i come over like an idiot. BUt i really can't figure out where to place it.. It might be because i'm Dutch an Lua is based on English. There's a little Language Barrier. HeadlessDeathSpeaker 9 — 5y
0
@HeadlessDeathSpeaker Players is a service, so if you see it out of context like this you should add "local Players = game:GetService('Players')" near the top of your script to apply it. Optikk 499 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Use game:GetService("Players"). An example of this is:

local Players = game:GetService("Players")
local function getPlayerByUserId(userId)
    for _, player in pairs(Players:GetPlayers()) do
        if player.UserId == userId then
            return player
        end
    end
end
0
I know that script. but i don't know how to merge it with my current script.... HeadlessDeathSpeaker 9 — 5y
0
just use "Dear " .. getPlayerByUserId(bannedplayerid).Name .. "You have been banned." bryancololee 0 — 5y
0
@Bryancololee. If i place that on in the script i get the errormessage 'Attempt to call Global 'getplayerbyuserid' (a nil value) Stack Begin' HeadlessDeathSpeaker 9 — 5y
0
Thats weird. bryancololee 0 — 5y
0
Try removing the local in line 2. bryancololee 0 — 5y

Answer this question