hello i am trying to get the players userid from name but it wont load after like 1 try i’ve even tried to add a pcall. so i am wondering if theres any other way to get a players userid from name without the :GetUserIdFromNameAsync() or whatever it is.
Nah the only way to get the player id by name is from getuseridfromname. Maybe show your code and we can see what the issue is?
it’s pretty long but the main thing i’m doing is:
local UserId
local success, err = pcall(function()
UserId = game:GetService("Players"):GetUserIdFromNameAsync(TargetName)
end)
if success then
print(success, UserId)
else
error(err)
end
i’m pretty certain i did it the most efficient way, but it still wont load and i have no idea why
Yep already found your issue. Hold on.
Actually nah didn’t spot your error, tell me what type of script is it. Are you sure its enabled? And is it in some sort of event? And where is it parented to?
yeah, i’m making a gift system, kind of like pls donate but i’m using the userid to save the players datastore, and i can’t really see any other way i could get the userid other than this. it’s getting fired from a remote on the server and the script is located in serverscriptservice.
Could you show the full script then? Including the remotes? It would be helpful so that we can figure out the problem.
of course here:
local manager = require(game.ServerScriptService.DataStoreProfiles.PlayerData.Manager)
local GiftingModule = require(game:GetService("ServerScriptService").gifting.GiftingModule)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
function comma(amount)
while true do
local k
amount, k = string.gsub(amount, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k == 0) then
break
end
end
return amount
end
local comma1 = require(game.ReplicatedStorage.Packages.NumberHelpersABB)
function naturalTime(p1, Player)
if not p1 then
return "unable to get time"
end
local v2 = DateTime.now().UnixTimestamp - p1;
if v2 < 10 then
return "Just now";
end;
if v2 < 60 then
return string.format("%i seconds ago", v2);
end;
if v2 < 120 then
return "a minute ago";
end;
if v2 < 3600 then
return string.format("%i minutes ago", v2 / 60);
end;
if v2 < 7200 then
return "an hour ago";
end;
if v2 < (7200 * 12) then
return string.format("%i hours ago", v2 / 3600);
end;
if v2 < (7200 * 24) then
return "a day ago";
end;
if v2 <= (7200 * 4380) then
return string.format("%i days ago", v2 / (3600 * 24));
end;
if v2 < (7200 * (4380 * 2)) then -- 4380 is one year, 4380 * 2 would be two years.
return "a year ago";
end;
if v2 >= (7200 * (4380 * 2)) then -- 4380 is one year, 4380 * 2 would be two years.
return string.format("%i years ago", v2 / (3600 * 8760));
end;
return "failed to get date"
end
local ProfileService = require(script.Parent.Parent.DataStoreProfiles.Library.ProfileService)
local TemplateData = require(script.Parent.Parent.DataStoreProfiles.PlayerData.Template)
local ProfileStore = ProfileService.GetProfileStore("ProfileStore_695421055556556551", TemplateData)
local SortingService = require(script.Parent.Parent.SortingService)
local function Update(TargetId, LeaderstatName, Amount)
task.spawn(function()
SortingService:UpdateData(TargetId, LeaderstatName, Amount, "Daily", "day")
end)
task.spawn(function()
SortingService:UpdateData(TargetId, LeaderstatName, Amount, "Weekly", nil)
end)
task.spawn(function()
SortingService:UpdateData(TargetId, LeaderstatName, Amount, "Monthly", "month")
end)
end
function GiveUnclaimedTemplate(Player, Gift)
local Template = ReplicatedStorage.UnclaimedTemplate:Clone()
Template.Parent = Player.PlayerGui.ScreenGui.GiftInbox.Frame.Leaderboard
Template:SetAttribute("Amount", Gift[3])
local DisplayName = Gift[8]
Template.Gift.Avatar.Image = Players:GetUserThumbnailAsync(Gift[9], Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
Template.Gift.Info.Username.Text = DisplayName..' <font transparency="0.5">@'..Gift[2]..'</font>'
Template.Gift.Info.Amount.Text = "Gifted \238\128\130"..comma1.Comma(Gift[3])..""
Template.Gift.Info.Message.Text = Gift[4]
Template.Gift.Info.Time.Text = naturalTime(Gift[5], Player)
if Gift[3] < 1000 then
Template.BackgroundColor3 = Color3.fromRGB(33, 227, 111)
elseif Gift[3] < 10000 then
Template.BackgroundColor3 = Color3.fromRGB(255, 191, 0)
elseif Gift[3] < 100000 then
Template.BackgroundColor3 = Color3.fromRGB(0, 179, 255)
elseif Gift[3] < 1000000 then
Template.BackgroundColor3 = Color3.fromRGB(255, 0, 230)
elseif Gift[3] >= 1000000 then
Template.BackgroundColor3 = Color3.fromRGB(255, 0, 115)
end
local TweenTimeIn = 0.2
local TweenTimeOut = 0.4
Template.MouseEnter:Connect(function()
Template.Hover.UIPadding.PaddingRight = UDim.new(0, 0)
Template.Hover.Claim.TextTransparency = 1
Template.Hover.Tap.TextTransparency = 1
TweenService:Create(Template.Hover.UIPadding, TweenInfo.new(TweenTimeIn), {PaddingRight = UDim.new(0.1, 0)}):Play()
TweenService:Create(Template.Hover.Tap, TweenInfo.new(TweenTimeIn), {TextTransparency = 0}):Play()
TweenService:Create(Template.Hover.Claim, TweenInfo.new(TweenTimeIn), {TextTransparency = 0}):Play()
TweenService:Create(Template.Hover, TweenInfo.new(TweenTimeIn), {BackgroundTransparency = 0}):Play()
end)
Template.MouseLeave:Connect(function()
TweenService:Create(Template.Hover.Tap, TweenInfo.new(TweenTimeOut), {TextTransparency = 1}):Play()
TweenService:Create(Template.Hover.Claim, TweenInfo.new(TweenTimeOut), {TextTransparency = 1}):Play()
TweenService:Create(Template.Hover, TweenInfo.new(TweenTimeOut), {BackgroundTransparency = 1}):Play()
end)
Template.MouseButton1Click:Connect(function()
if Player:GetAttribute("CanClaim") == true then
task.spawn(function()
Player:SetAttribute("CanClaim", false)
task.wait(3.5)
Player:SetAttribute("CanClaim", true)
end)
Template:Destroy()
Player.PlayerGui.ScreenGui.GiftLocal.Gifts.Value -= 1
if Player.PlayerGui.ScreenGui.GiftLocal.Gifts.Value <= 0 then
Player.PlayerGui.ScreenGui.Donate.Buttons.Inbox.Count.Text = 0
Player.PlayerGui.ScreenGui.Donate.Buttons.Inbox.Count.Visible = false;
end
manager.AddStat(Player, "Raised", Gift[3])
manager.AddStat(Player, "Robux", Gift[3] * 0.7)
manager.AddStat(Player, "Giftbux", math.floor(math.sqrt(Gift[3]) * 2))
GiftingModule.ClaimGift(Player.UserId, Gift[6])
Update(Player.UserId, "Raised", tonumber(Gift[3]))
game.ReplicatedStorage.Events.Popup:FireClient(Player, "claimed gift!", Color3.fromRGB(24, 255, 97), "Success")
else
game.ReplicatedStorage.Events.Popup:FireClient(Player, "please slow down", Color3.fromRGB(255, 255, 255), "Fail")
return
end
end)
wait()
end
Players.PlayerAdded:Connect(function(Player)
local CanClaim = Player:SetAttribute("CanClaim", true)
local CanGift = Player:SetAttribute("CanGift", true)
local CanRefresh = Player:SetAttribute("CanRefresh", true)
repeat
task.wait(5)
until Player.PlayerGui
--task.wait(3)
local Gifts = GiftingModule.GetGifts(Player.UserId)
if Gifts then
Player.PlayerGui:WaitForChild("ScreenGui").GiftLocal.Gifts.Value = #Gifts
for _, v in pairs(Player.PlayerGui.ScreenGui.GiftInbox.Frame.Leaderboard:GetChildren()) do
if v:IsA("TextButton") then
v:Destroy()
end
end
if Gifts then
for _, Gift in pairs(Gifts) do
GiveUnclaimedTemplate(Player, Gift)
if Gift[7] == false then
GiftingModule.UpdateSeen(Player.UserId, Gift[6])
spawn(function()
game.ReplicatedStorage.Events.Popup:FireClient(Player, "you received a ".. comma1.Comma(Gift[3]) .." gift from @"..Gift[2], Color3.fromRGB(255, 38, 132), "Donation")
wait(2)
end)
end
task.wait()
end
end
end
end)
local TextService = game:GetService("TextService")
local function filterText(player, text)
local filteredInstance = TextService:FilterStringAsync(text, player.UserId, Enum.TextFilterContext.PublicChat)
local filteredString = filteredInstance:GetNonChatStringForUserAsync(player.UserId)
return filteredString
end
ReplicatedStorage.Events.SendGift.OnServerEvent:Connect(function(Player, Target, Amount, Message)
local TargetUserId = game.Players:GetUserIdFromNameAsync(Target)
local TargetName = Target
if not Player:GetAttribute("CanGift") or not tonumber(Amount) or not Player or TargetUserId <= 0 or Amount <= 0 then print(Player:GetAttribute("CanGift"), tonumber(Amount), Player, TargetUserId, Amount) game.ReplicatedStorage.Events.Popup:FireClient(Player, "an error occurred, please try again.", Color3.fromRGB(255, 255, 255), "Fail") return end
if manager.getStat(Player, "Robux") >= Amount and Player:GetAttribute("CanGift") == true and tonumber(Amount) and Player and TargetUserId > 0 and Amount > 0 then
Amount = tonumber(Amount)
spawn(function()
Player:SetAttribute("CanGift", false)
task.wait(3.5)
Player:SetAttribute("CanGift", true)
end)
game.ReplicatedStorage.Events.Popup:FireClient(Player, "sent gift!", Color3.fromRGB(24, 255, 43), "Donation")
manager.MinusStat(Player, "Robux", Amount)
manager.AddStat(Player, "Giftbux", math.floor(math.sqrt(Amount) * 5))
manager.AddStat(Player, "Donated", Amount)
Update(Player.UserId, "Donated", Amount)
local RandomID = math.random(0, 1000000)
pcall(function()
GiftingModule.SaveGifts(TargetUserId, {TargetName, Player.Name, Amount, filterText(Player, Message), os.time(), RandomID, false, Player.DisplayName, Player.UserId})
end)
local Data = {
["DonaterName"] = Player.Name,
["DonaterUserId"] = Player.UserId,
["ReceiverName"] = TargetName,
["ReceiverUserId"] = TargetUserId,
["Amount"] = Amount,
["RandomID"] = RandomID
}
game:GetService("MessagingService"):PublishAsync("Gift", Data)
elseif manager.getStat(Player, "Robux") < Amount then
game.ReplicatedStorage.sick:FireClient(Player, Amount)
return
end
end)
game.ReplicatedStorage.Events.RefreshGifts.OnServerEvent:Connect(function(Player)
if Player:GetAttribute("CanRefresh") then
Player:SetAttribute("CanRefresh", false)
for _, v in pairs(Player.PlayerGui.ScreenGui.GiftInbox.Frame.Leaderboard:GetChildren()) do
if v:IsA("TextButton") then
v:Destroy()
end
end
local Gifts = GiftingModule.GetGifts(Player.UserId)
if Gifts then
for _, Gift in pairs(Gifts) do
GiveUnclaimedTemplate(Player, Gift)
task.wait()
end
end
task.spawn(function()
task.wait(1)
Player:SetAttribute("CanRefresh", true)
end)
end
end)
game:GetService("MessagingService"):SubscribeAsync("Gift", function(Data)
local DonaterName, DonaterUserId, ReceiverName, ReceiverUserId, Amount, RandomID = Data.Data["DonaterName"], Data.Data["DonaterUserId"], Data.Data["ReceiverName"], Data.Data["ReceiverUserId"], Data.Data["Amount"], Data.Data["RandomID"]
if game.Players:FindFirstChild(ReceiverName) then
game.Players:FindFirstChild(ReceiverName).PlayerGui.ScreenGui.GiftLocal.Gifts.Value = 0
for _, v in pairs(game.Players:FindFirstChild(ReceiverName).PlayerGui.ScreenGui.GiftInbox.Frame.Leaderboard:GetChildren()) do
if v:IsA("TextButton") then
v:Destroy()
end
end
local Gifts = GiftingModule.GetGifts(ReceiverUserId)
if Gifts then
for _, Gift in pairs(Gifts) do
game.Players:FindFirstChild(ReceiverName).PlayerGui.ScreenGui.GiftLocal.Gifts.Value += 1
GiveUnclaimedTemplate(game.Players:FindFirstChild(ReceiverName), Gift)
if Gift[7] == false then
--print("show unseen popups")
game.ReplicatedStorage.Events.Popup:FireClient(game.Players:FindFirstChild(ReceiverName), "you received a ".. comma1.Comma(Gift[3]) .." gift from @"..Gift[2], Color3.fromRGB(255, 38, 132), "Donation")
GiftingModule.UpdateSeen(ReceiverUserId, Gift[6])
end
task.wait(0)
end
end
end
end)
UserId = game.Players.PlayerName.UserId
??? – type == number
yeah oops i was testing something haha my bad
Maybe that was a bad idea, just show me the lines where you are firing to either server/client lmao. I think the issue may be due to the client/server communication.
Could you also seperate in your script which one is local and which one is server-sided? Just for ease?
all i’m doing on the client is just firing the TargetName but heres the recieving on the server
ReplicatedStorage.Events.SendGift.OnServerEvent:Connect(function(Player, Target, Amount, Message)
local TargetUserId
local success, err = pcall(function()
TargetUserId = game:GetService("Players"):GetUserIdFromNameAsync(Target)
end)
if success then
local TargetName = Target
if not Player:GetAttribute("CanGift") or not tonumber(Amount) or not Player or TargetUserId <= 0 or Amount <= 0 then print(Player:GetAttribute("CanGift"), tonumber(Amount), Player, TargetUserId, Amount) game.ReplicatedStorage.Events.Popup:FireClient(Player, "an error occurred, please try again.", Color3.fromRGB(255, 255, 255), "Fail") return end
if manager.getStat(Player, "Robux") >= Amount and Player:GetAttribute("CanGift") == true and tonumber(Amount) and Player and TargetUserId > 0 and Amount > 0 then
Amount = tonumber(Amount)
spawn(function()
Player:SetAttribute("CanGift", false)
task.wait(3.5)
Player:SetAttribute("CanGift", true)
end)
game.ReplicatedStorage.Events.Popup:FireClient(Player, "sent gift!", Color3.fromRGB(24, 255, 43), "Donation")
manager.MinusStat(Player, "Robux", Amount)
manager.AddStat(Player, "Giftbux", math.floor(math.sqrt(Amount) * 5))
manager.AddStat(Player, "Donated", Amount)
Update(Player.UserId, "Donated", Amount)
local RandomID = math.random(0, 1000000)
pcall(function()
GiftingModule.SaveGifts(TargetUserId, {TargetName, Player.Name, Amount, filterText(Player, Message), os.time(), RandomID, false, Player.DisplayName, Player.UserId})
end)
local Data = {
["DonaterName"] = Player.Name,
["DonaterUserId"] = Player.UserId,
["ReceiverName"] = TargetName,
["ReceiverUserId"] = TargetUserId,
["Amount"] = Amount,
["RandomID"] = RandomID
}
game:GetService("MessagingService"):PublishAsync("Gift", Data)
elseif manager.getStat(Player, "Robux") < Amount then
game.ReplicatedStorage.sick:FireClient(Player, Amount)
return
end
else
error(err)
end
end)
Strange, the script works perfectly fine for me
local Success, UserId = pcall(function()
return game:GetService("Players"):GetUserIdFromNameAsync(Player.Name)
end)
print(UserId)
yea it works fine the first couple times but then after some time it slowly breaks and doesn’t load
local success, err = pcall(function()
TargetUserId = game:GetService("Players"):GetUserIdFromNameAsync(Target)
end)
I may be stupid but, is there a reason you are getting the player by the user id? I mean from the remote event you’ve already got the player instance. Can’t you just do player.UserId or something?
yea there is, i’m saving a datastore by userid and the Target is the player the person is gifting. so the Player is the player donating and Target is the reciever.
Ah I think I see why thats happening, it probably breaks after some time due to all the task.spawns and while loops you are using. You can’t use too many datastore getasync/setasync or else it’ll stop setting the data and getting the data for that key.
Yeah sorry lol I didn’t really read your full script because jesus. But I did see you are using many while loops/task.spawn functions which may be the reason why your script slowly breaks. Reduce them.
hello, what won’t load? the userId? also are you getting any errors in output?
:GetUserIdFromNameAsync()
querys the website, and is rate limited
so if you do other things in your game like using :GetNameFromUserIdAsync()
to load player names into a leaderboard, you may be hitting the limit.