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.
What is the issue?
On Line 5 the console is saying: Unable to cast string to int64
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)
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.
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 --
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)
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)