Get UserID of an offline user

Hello, I’ve recently used tables to store data such as if a user has a warning under there name. What if I want to clear the warnings off an offline user? The table saves and loads using the userId then a key that I can use to reset data. I was wondering if it’d be possible to get the userId of a user who is offline based on the name and then load in there data in the table to then remove the warnings or bans.

Something like this…

PunishmentsStorage:GetAsync(target…punishmentKey)
punishments[Target][‘Warnings’] = 0`

 local userID = game.Players:GetUserIdFromNameAsync("Jamie_Jr")
9 Likes

Don’t forget to wrap it in pcall!

7 Likes

Hm, the userId from name Async seems to work. Although when firing the client (for a check) it always returns nil. Even when I make sure it says return true. I added prints to debug and everything seems like it should work fine. The client has return true… the server prints that its nil. I assume this isn’t to do with the userId but is there a reason for this?

Also, would this work if the user is offline/not in game?

While attempting to do your idea it didn’t seem to work.

_G.ClearPunishments = function(plr,target)
	pcall(function()
		print(game:GetService("Players"):GetUserIdFromNameAsync(target))
		if game:GetService("Players"):FindFirstChild(target) then else
			Punishments[target] = PunishmentsStorage:GetAsync(game:GetService("Players"):GetUserIdFromNameAsync(target)..punishmentKey)
		end
		Punishments[target]['Warnings'] = 0
		Punishments[target]['Banned'] = false
		Punishments[target]['BanReason'] = "None"
		PunishmentsStorage:SetAsync(game:GetService("Players"):GetUserIdFromNameAsync(target)..punishmentKey, Punishments[target])
		if game:GetService("Players"):FindFirstChild(target) then else
			Punishments[target] = nil
		end
	end)
end

It should work; try testing it outside of that code with some random name.

Then if it works figure out what in your own code isn’t working.

Something is bothering me about this code, in other news. Did you create a table for the user, or is a table saved to the datastore? If not, you can’t start assigning keys and values out of the blue.

Punishments[target] = {}
Punishments[target]["Warnings"] = blah whatever

GetAsync whatever whatever
-> Should return a table