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 6 years ago
Edited 6 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.

01bannedplayerid = script.UserID.Value--the person to ban (userid e.g. 12345678)
02banreason = script.BanReason.Value --reason for ban
03anonymousbanner = false --if the banner is anonymous (e.g false)
04banner = "Tom" --put whoever banned the person here
05enforceban = true --if the ban is enforced (e.g true)
06note = script.BanAppeal.Value --e.g. message someone to appeal
07secretban = false --if when banned it displays the ban message (false) or "You have lost connection to the game" (true)
08Alert = script.Parent.Alert -- Sound
09Alert2 = script.Parent.Alert2  -- Sound
10Username = script.Name -- Scripts name.
11BanStat = script.BanStatus.Value -- If user still is banned, or that i did unban.
12 
13--------------------------------------------------------------------------------------------------------------------------------------------------------
14 
15game.Players.PlayerAdded:connect(function(instance)
View all 42 lines...

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 — 6y
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 — 6y

2 answers

Log in to vote
0
Answered by
sydre2 25
6 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 — 6y
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 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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

1local Players = game:GetService("Players")
2local function getPlayerByUserId(userId)
3    for _, player in pairs(Players:GetPlayers()) do
4        if player.UserId == userId then
5            return player
6        end
7    end
8end
0
I know that script. but i don't know how to merge it with my current script.... HeadlessDeathSpeaker 9 — 6y
0
just use "Dear " .. getPlayerByUserId(bannedplayerid).Name .. "You have been banned." bryancololee 0 — 6y
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 — 6y
0
Thats weird. bryancololee 0 — 6y
0
Try removing the local in line 2. bryancololee 0 — 6y

Answer this question