How do I grab player ID's in a script? I seem to be stuck with my problem here.
01 | local BadgeService = game:GetService( "BadgeService" ) |
02 | local Players = game:GetService( "Players" ) |
03 | local UserID = Players.LocalPlayer.UserId |
04 |
05 | script.Parent.MouseButton 1 Down:Connect( function (player) |
06 | script.Parent.Parent.Click:Play() |
07 | if BadgeService:UserHasBadgeAsync(UserID, 2124620114 ) then |
08 | print ( "Giving gamepass to player" ) |
09 | script.LocalScript.RemoteEvent 2 :FireClient(player) |
10 | end |
11 | end ) |
Hello there! Is this a server script? If so, put the MouseButton1Down in a local script, and fire the remote event. That is my recommendation, and it may be necessary for filtering enabled.
To get the UserId of a player in a serverscript, use the PlayerAdded event. An example of it with your code is shown below:
01 | local BadgeService = game:GetService( "BadgeService" ) |
02 | local Players = game:GetService( "Players" ) |
03 |
04 | game.Players.PlayerAdded:Connect( function (plr) |
05 | local UserID = plr.UserId |
06 | script.Parent.MouseButton 1 Down:Connect( function () |
07 | script.Parent.Parent.Click:Play() |
08 | if BadgeService:UserHasBadgeAsync(UserID, 2124620114 ) then |
09 | print ( "Giving gamepass to player" ) |
10 | script.LocalScript.RemoteEvent 2 :FireClient(plr) |
11 | end |
12 | end ) |
13 | end ) |
The reason why this works is you define a parameter called player which is the player that has been added into the game. Therefore, you can use this parameter to access the player.
Again, I highly recommend that you change your script drastically if it is a ServerScript. I hope this helped. Have a nice day!
Now, this has a syntax error, meaning you typed the code wrong.
Now if it is a local script you can not do game.Players.LocalPlayer because that is nil on server, and if it is a local script then you can not do :FireClient().
If you do want to do FireClient because it is a local script. Then replace the RemoteEvent with a BindableEvent, and do :Fire(), and when receiving the signal do .Event:Connect().
If it is a server script, do someting like this.
01 | local BadgeService = game:GetService( "BadgeService" ) |
02 | local Players = game:GetService( "Players" ) |
03 |
04 | script.Parent.MouseButton 1 Down:Connect( function (player) |
05 | script.Parent.Parent.Click:Play() |
06 | local UserID = player.UserId |
07 | if BadgeService:UserHasBadgeAsync(UserID, 2124620114 ) then |
08 | print ( "Giving gamepass to player" ) |
09 | script.LocalScript.RemoteEvent 2 :FireClient(player) |
10 | end |
11 | end ) |
I hope this helps!