Help with badge giving gui

hello!
i want to make a text box gui where if i type someones nickname, he gets awarded a badge. i tried to get user id by username, but it doesnt work. i didnt find the solution anywhere. dev console says: “Players:GetUserIdFromNameAsync() failed: Unknown user”. here is my script in ServerScriptService:

local FunnyBadgeID = 2124770603
local FunnyPlayerUsername = game.StarterGui.FunnyBadgeGiver.Frame.TextBox.Text
local FunnyPlayerID = game.Players:GetUserIdFromNameAsync(FunnyPlayerUsername)

game:GetService("BadgeService"):AwardBadge(FunnyPlayerID, FunnyBadgeID)
2 Likes

If you are testing it on studio, it would likely to error since your UserId ranges from 0 and below on studio testing. You should try testing it by publishing and joining the game.

nope, still doesnt work. but in game and not in studio dev console doesnt say anything at all.

Try printing FunnyPlayerUsername and see what’s being printed. If it prints out something other than username, you should provide the full code including part that sets text to the username or whatsoever

i added “print(FunnyPlayerUsername)” to the script and it doesnt print anything.

I need to take a look at the part that sets TextBox.Text to player’s username

here is my starter gui folder:
gui
and this is the local script inside of surface gui:

if game.Players.LocalPlayer.Name == "Dude8248" then --put in ur username
	script.Parent.Enabled = true
else
	script.Parent.Enabled = false
end

and here is my server script service folder:
script service
that FunnyBadgeGiver script is the script that i showed before

i dont have anything else related to this anywhere. lso you type the players username in the textbox.

So you mean game.StarterGui.FunnyBadgeGiver.Frame.TextBox.Text is never set and is always blank?

i dont really understand what you mean.

StarterGui isn’t changed when users interact with their GUI. Get the local player’s GUI instead.

-- replace UI with your own UI instance
local gui = game.Players.LocalPlayer:WaitForChild('UI')

Then use remotes to make the server award the badge.

do i put local gui = game.Players.LocalPlayer:WaitForChild('UI') inside the local script in screen gui? also, how do i use remotes since im not really good at studio?

Yes, but change the UI part with your text box. To use remotes, place a RemoteEvent in ReplicatedStorage, then follow the example:

-- server
local repl = game:GetService("ReplicatedStorage")
local rem =  repl:WaitForChild("Remote")

rem.OnServerEvent:Connect(function(player, data)
	print(player.Name, data)
end)
-- client
local repl = game:GetService("ReplicatedStorage")
local rem =  repl:WaitForChild("Remote")

-- "data" is the data you want to send, this is only an example
local data = "abc"

rem:FireServer(data)

The example prints your username, then “abc” when the localscript fires the remote. The player part in the server is the player object of the player that fired the remote. The remote can have any name, just make sure to edit the example accordingly.

where do i put this tho

-- client
local repl = game:GetService("ReplicatedStorage")
local rem =  repl:WaitForChild("Remote")

-- "data" is the data you want to send, this is only an example
local data = "abc"

rem:FireServer(data)

That’s a LocalScript, as the first comment states that it should go in the client.

you mean players folder or what?

Just modify your script to include the example.

i did something and it gives me an error: “ServerScriptService.FunnyBadgeGiver: 1: attempt to index nil with “WaitForChild””

Did you edit that part to wait for your remote instead of the example? As in, does WaitForChild() and your remote have the same name?

i think i did. also, what do i change “abc” data to?

The name that you’re trying to send.