How to fix random user id?

Its a number, and I type 1500 in it, so the server know how much to give the player from the game wallet of player

can you please show us the textbox properties, maybe somethings messing it up

1 Like

ok so use this

local group={tonumber(game.Players:GetUserIdFromNameAsync(script.Parent.TextBox.Text),script.Parent.MoneyValueBox.Text}
game.ReplicatedStorage.SendMoneyEvent:FireServer(group)

then in Server script

use .OnServerEvent(plr,group)
local toplr=group[1]
print(toplr)

end)

1 Like

robloxapp-20211011-1928130.wmv (333.6 KB)
Also here is how it works, its a mobile like thingy

no im asking for the textbox properties

1 Like

can you screenshot it? maybe something will mess it up

1 Like

This thread’s replies are getting insanely bloated. Please do some of your own testing on the solutions you’re proposing or try getting more information so you can look more closely at the problem. There’s a lot of prospective solutions being thrown around that don’t mean much. Please also try to condense posts: the DevForum isn’t a real-time chat room, you can condense small posts into one.

For OP: could you give more context as to the full scope of your environment? I made a quick repro in Studio that mocks up almost exactly what you’re working with given the information provided in the thread and I’m not receiving “random UserIds”. Perhaps you could compare your code and/or method of testing with my repro and see where the difference is, which may help you determine where the issue is? It works as intended, assuming no invalid inputs.

Transfer Repro.rbxl (35.8 KB)

There is no error handling. To use it, type a username first and then a dollar value and then hit send. Check your output or developer console to see respective prints from the client and the server as to what its receiving. The client will print what it will attempt to give the server and the server will print what it received from the client.

It’s far more useful to work with a repro and some context than doing a shot-in-the-dark roulette with a bunch of untested proposals.

1 Like

image
image
image
image
image

dont make it selectable, i tried it and it broke my script long time ago

Yes, I had tried this many times, it prints correct userid, but this time it printed random so I posted it

The script in server prints its a string

game.ReplicatedStorage.SendMoneyEvent.OnServerEvent:Connect(function(plr,toplr,totalmoney)
    print(toplr)
    print(totalmoney)
    local recdata = ds1:GetAsync(toplr)
    local plrname = game.Players:GetNameFromUserIdAsync(toplr)
    if plr.PlayerStats.Money.Value >= totalmoney -- 1st is intvalue and I kept money.value so I get the number, and second is number then
        plr.PlayerStats.Money.Value -= totalmoney
        wait(5)
        if ds1:GetAsync(toplr) then
            ds1:SetAsync(toplr,recdata + totalmoney)
        elseif game.Players:FindFirstChild(plrname) then
            game.Players:FindFirstChild(plrname).PlayerStats.Money.Value += totalmoney
        end
    end
end)

Both of them are numbers

check if its a number and then tonumber it and mayb it will work

tonumber returned nil, and when I printed it, its a number, but still it says error

extremely wierd, maybe before sending it to the server add an TEMPORARY intvalue put the number in it, and then send the TEMPORARY intvalue’s value to the server than remove the intvalue

2 Likes

You cannot send an instance, in a local script
I guess

maybe only send the name and thats it?

1 Like

because it would be alot easier than userid

1 Like

IDEA - instead doing it in a local, do the intvalue thing in server

2 Likes

This function can error - and I wonder if what is being returned is not an Id but some part of an error being converted to a number, like a date/time etc.

Instead of the Client, call the GetUserIdFromNamesAsync function on the Server instead and wrap it in a pcall as suggested on the Developer Hub.
Players:GetUserIdFromNameAsync (roblox.com)

You can learn more about pcall here Lua Globals (roblox.com)

local Players = game:GetService("Players")
 
-- Memoization: since these results are rarely (if ever) going to change
-- all we have to do is check a cache table for the name.
-- If we find the name, then we have no work to do! Just return the user id (fast).
-- If we don't find the name (cache miss), go look it up (takes time).
local cache = {}
function getUserIdFromUsername(name)
	-- First, check if the cache contains the name
	if cache[name] then return cache[name] end
	-- Second, check if the user is already connected to the server
	local player = Players:FindFirstChild(name)
	if player then
		cache[name] = player.UserId
		return player.UserId
	end 
	-- If all else fails, send a request
	local id
	pcall(function ()
		id = Players:GetUserIdFromNameAsync(name)
	end)
	cache[name] = id
	return id
end

Pls use tonumber only for one variable ,but you cannt mix two variables in tonumber here is your code tonumber(game.Players:GetUserIdFromNameAsync(script.Parent.TextBox.Text),script.Parent.MoneyValueBox.Text)
it gives different result man use

tonumber(game.Players:GetUserIdFromNameAsync(script.Parent.TextBox.Text),tonumber(script.Parent.MoneyValueBox.Text)

btw you dont need to use tonumber to get userid cuz user id is already a numbernumber

2 Likes