Unable to cast string to int64

  1. What do you want to achieve? I’m trying to make it so when the player puts the asset ID into the textbox, the hat is worn by player.

  2. What is the issue?
    image
    On Line 5 the console is saying: Unable to cast string to int64

  3. What solutions have you tried so far? I have tried searching for similar posts to mine but I couldn’t fine any solutions

local insertService = game:GetService("InsertService")

event.OnServerEvent:Connect(function(player, id)
	local model = insertService:LoadAsset(id)
	model.Parent = workspace
	local asset = model:GetChildren()[1]

	if asset:IsA("Accessory") then
		player.Character.Humanoid:AddAccessory(asset)
end
end)
1 Like


In your code, id is most likely passed over as a string. You can fix this by passing over an actual number or use tonumber() for the string.

2 Likes

This is my code for my local script:
image
I’m trying to tonumber() the input text but the console sends this error out:
Argument 1 missing or nil

1 Like

What does asset print out as?

1 Like

Asset prints out as nothing
image

1 Like

That means that text is nil. Make sure that you are accessing the right thing.

1 Like

The text shouldn’t be nil. I am putting the ID in the textbox.
image

1 Like

Have you tried printing out text? Also, are you using a server script for this script:
image

1 Like

i tried printing out id and text, both of them sent out nils. The above script is a local script inside of a button. When I place it into serverscriptstorage it didn’t work.

1 Like

That means that script.Parent.Parent.Hat.Text is nil, so you need to change it to actually get the hat Id.

Edit: Just realised, you never update the text variable, so it will always be nil, even if you type in something new.

1 Like

Is there a way I can update it?

local textBox = script.Parent.Parent.Hat
local text = textBox.Text
local id = tonumber(text)
textBox:GetPropertyChangedSignal("Text"):Connect(function(newText)
    text = newText
    id = tonumber(text)
end)
-- rest of your code here --

I’ve tried printing text and id out. It returns with nil

it is kinda simple, Change

local textBox = script.Parent.Parent.Hat.Text

to

local textBox = script.Parent.Parent.Hat

so ending code should be something like this

local textBox = script.Parent.Parent.Hat
local event = game.ReplicatedStorage:WaitForChild("HatInserter")
local button = script.Parent
button.MouseButton1Click:Connect(function()
        local id = tonumber(textBox.Text)
        event:FireServer(id)
end)

You’re currently declaring a variable which stores the textbox’s text once (when the script first executes), you need to instead declare a variable for the textbox object itself so that you can index its ‘Text’ property whenver necessary, for example.

local TextBox = script.Parent

print(TextBox.Text)
task.wait(1)
print(TextBox.Text)

Then you want to get the text when the event is about to fire, not constantly updates it.

local textBox = script.Parent.Parent.Hat
local event = game.ReplicatedStorage:WaitForChild("HatInserter")
local button = script.Parent

button.MouseButton1Down:Connect(function()
    -- get the text in here
    local text = textBox.Text
    local id = tonumber(text)

    event:FireServer(id)
end)

You can also follow @.msix29’s code.

Yes this does work but I need to also have it so they can add multiple hats aswell. Would that need a massive code change? If it does than I won’t add it in.

I don’t understand what you mean exactly by multiple hats but I think you mean something like
id1,id2 which will let them equip both of them so here is how to do it, you need to check if the text has a character which splits the IDs so something like this

local splitCharacter = ","

local function CheckIfMultipleIDs(Text)
	if string.find(Text , splitCharacter) then
		local IDs = Text:split(splitCharacter)
		return true , IDs -- IDs[1] = first id, IDs[2] = second id and etc
	end
end

and now we need to just use it in the script so the ending script is supposed to be something like this NOTE : I didn’t test the code so you might need to change some stuff or tell me if you got any errors and I’ll help you fix them

local splitCharacter = ","

local function CheckIfMultipleIDs(Text)
	if string.find(Text , splitCharacter) then
		local IDs = Text:split(splitCharacter)
		return true , IDs -- IDs[1] = first id, IDs[2] = second id and etc
	end
end

local textBox = script.Parent.Parent.Hat
local event = game.ReplicatedStorage:WaitForChild("HatInserter")
local button = script.Parent

button.MouseButton1Click:Connect(function()
	local foundMultipleIDs , allIDs = CheckIfMultipleIDs(textBox.Text)
	
	if not foundMultipleIDs then
		local id = tonumber(textBox.Text)
		event:FireServer(id)
		
	elseif foundMultipleIDs then
		for _ , v in pairs(allIDs) do
			local id = tonumber(textBox.Text)
			event:FireServer(id)
		end
	end
end)

The text is nil because you capture it into a variable before the player inputs the text. Fix this by putting the “id = blabla” line inside the button event.

I’ve tried this and it works with one hat but not with two. I’m pasting the IDs like this ‘5100703261,4735714475’ and it doesn’t work. The console says ’ Argument 1 missing or nil’
It then points to local model = insertService:LoadAsset(id)