Unable to cast string to int64

Im trying to make a script when an user enters someones user’s ID, it loads their avatar.
How it works is the user enters an ID on a textbox, puts that ID into a StringValue, and code does it’s thing.
But i get the error Unable to cast string to int64.

What im trying to make is a meme game. I’ve looked at posts multiple times and I still haven’t found an issue, please help.

Textbox code:

local id = script.Parent.Parent.FirstID.Text
local plr = game.Players

local event = game.ReplicatedStorage.LoadPlayer1
local button = script.Parent.Parent.FirstApply

local firstguymodel = workspace.FirstGuy

button.MouseButton1Click:Connect(function()
	wait(1)
	event:FireServer()
	firstguymodel:WaitForChild("Id").Value = tostring(id)
end)

Applying the avatar code:

local model = script.Parent
local humanoid = model:WaitForChild("Humanoid")
local event = game.ReplicatedStorage.LoadPlayer1
local ID = script.Parent.Id.Value

event.OnServerEvent:Connect(function()
	wait(1)
	local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(ID)
	humanoid:ApplyDescription(description)
end)
1 Like

ID needs to be a number not a string. Convert it to a number by using the tonumber function as shown below

Change Applying the avatar code to this:

local model = script.Parent
local humanoid = model:WaitForChild("Humanoid")
local event = game.ReplicatedStorage.LoadPlayer1
local ID = script.Parent.Id.Value

event.OnServerEvent:Connect(function()
	wait(1)
	local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(tonumber(ID))
	humanoid:ApplyDescription(description)
end)
1 Like

I have tried this before MULTIPLE times.
Now I am getting the agonizing error “Argument 1 missing or nil”

1 Like

Can you show a picture of the console when you implement the tonumber function? I want to see which line is erroring.

1 Like

1 Like

It’s because you are changing the ID value on the client and trying to access it on the server.

Change your Textbox code to this:

local id = script.Parent.Parent.FirstID.Text
local plr = game.Players

local event = game.ReplicatedStorage.LoadPlayer1
local button = script.Parent.Parent.FirstApply

local firstguymodel = workspace.FirstGuy

button.MouseButton1Click:Connect(function()
	wait(1)
	event:FireServer(id)
	firstguymodel:WaitForChild("Id").Value = tostring(id)
end)

Change your avatar code to this:

local model = script.Parent
local humanoid = model:WaitForChild("Humanoid")
local event = game.ReplicatedStorage.LoadPlayer1
local ID = script.Parent.Id.Value

event.OnServerEvent:Connect(function(id)
	wait(1)
	local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(tonumber(id))
	humanoid:ApplyDescription(description)
end)
2 Likes

still getting the exact same error. from the exact same line.

It’s because when we fire to the server, we fired the variable that we define outside of the MouseButton1Click callback, we don’t need to find the variable and can just fire it in the RBXScriptConnection as shown below.

Try this then
Textbox code:

local Players = game:GetService("LocalPlayer")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local localPlayer = Players.LocalPlayer :: LocalPlayer

local loadPlayerRemoteEvent = ReplicatedStorage.LoadPlayer1

local firstGuyModel = Workspace:WaitForChild("FirstGuy")

local applyButton = script.Parent.Parent.FirstApply

applyButton.Activated:Connect(function()
	task.wait(1)
	loadPlayerRemoteEvent:FireServer(script.Parent.Parent.FirstID.Text)
end)

Avatar Code:

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

local model = script.Parent
local humanoid = model:WaitForChild("Humanoid")
local loadPlayerRemoteEvent = ReplicatedStorage.LoadPlayer1

loadPlayerRemoteEvent.OnServerEvent:Connect(function(id)
	task.wait(1)
	local description = Players:GetHumanoidDescriptionFromUserId(tonumber(id))
	humanoid:ApplyDescription(description)
end)

I noticed some mistakes which I had to fix.
Still the same error ;(

I don’t see what the problem is, we can use print statements to debug it.

Textbox code:

local Players = game:GetService("LocalPlayer")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local localPlayer = Players.LocalPlayer :: LocalPlayer

local loadPlayerRemoteEvent = ReplicatedStorage.LoadPlayer1

local firstGuyModel = Workspace:WaitForChild("FirstGuy")

local applyButton = script.Parent.Parent.FirstApply

applyButton.Activated:Connect(function()
	task.wait(1)
	local idText = script.Parent.Parent.FirstID.Text
	print("Client", idText)
	loadPlayerRemoteEvent:FireServer(script.Parent.Parent.FirstID.Text)
end)

Avatar Code:

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

local model = script.Parent
local humanoid = model:WaitForChild("Humanoid")
local loadPlayerRemoteEvent = ReplicatedStorage.LoadPlayer1

loadPlayerRemoteEvent.OnServerEvent:Connect(function(id)
	task.wait(1)
	print("Server", id, humanoid)
	local description = Players:GetHumanoidDescriptionFromUserId(tonumber(id))
	humanoid:ApplyDescription(description)
end)

You forgot to add player as the first parameter on the OnServerEvent function

@DanielPlaysInROBLOX Put this in the server script

local Players = game:GetService("Players")
local model = script.Parent
local humanoid = model:WaitForChild("Humanoid")
local event = game.ReplicatedStorage.LoadPlayer1
local ID = script.Parent.Id.Value

event.OnServerEvent:Connect(function(player, id)
	task.wait(1)
	local description = Players:GetHumanoidDescriptionFromUserId(tonumber(id))
	humanoid:ApplyDescription(description)
end)
2 Likes

Thanks a lot dude!!! I’ve had this issue for hours you’re a life saver :Dㅤ

I would also thank @ahsanmoin05 since he got you most of the way there. I just fixed a small issue in his solution.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.