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

How do I grab player ID's in a script?

Asked by 3 years ago

How do I grab player ID's in a script? I seem to be stuck with my problem here.

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local UserID = Players.LocalPlayer.UserId

script.Parent.MouseButton1Down:Connect(function(player)
    script.Parent.Parent.Click:Play()
    if  BadgeService:UserHasBadgeAsync(UserID, 2124620114) then
        print("Giving gamepass to player")
        script.LocalScript.RemoteEvent2:FireClient(player)
    end
end)

2 answers

Log in to vote
1
Answered by 3 years ago

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:

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(plr)
    local UserID = plr.UserId
    script.Parent.MouseButton1Down:Connect(function()
        script.Parent.Parent.Click:Play()
        if BadgeService:UserHasBadgeAsync(UserID, 2124620114) then
            print("Giving gamepass to player")
            script.LocalScript.RemoteEvent2:FireClient(plr)
        end
        end)
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!

Ad
Log in to vote
0
Answered by
ew_001 58
3 years ago

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.

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

script.Parent.MouseButton1Down:Connect(function(player)
    script.Parent.Parent.Click:Play()
    local UserID = player.UserId
    if BadgeService:UserHasBadgeAsync(UserID, 2124620114) then
        print("Giving gamepass to player")
        script.LocalScript.RemoteEvent2:FireClient(player)
    end
end)

I hope this helps!

Answer this question