Unable to cast string to int64 error

So im making a custom hotbar script for my fighting game and im getting a strange error when getting to update the hotbar icons

The error itself is: “Unable to cast string to int64”

Heres my code
The error is on line 3

local function updatehotbar()
	for i,v in pairs(hotbarSlots:GetChildren()) do
		hotbar[v].SlotIcon.Image = "rbxassetid://" .. v.Icon.Value -- String value with the image id
		hotbar[v].CurrentSlot = v.Value
	end
end

for i,v in pairs(hotbarSlots:GetChildren()) do
	v.Changed:Connect(updatehotbar)
	v.Icon.Changed:Connect(updatehotbar)
end

Can someone help me fix this?

Could you paste your entire code?

Here it is, its not a lot

--// Inventory

local keybinds = {
	['1'] = "One",
	['2'] = "Two",
	['3'] = "Three",
	['4'] = "Four",
	['5'] = "Five",
}

local hotbarSlots = plr.Data:WaitForChild("Hotbar")
local hotbar = plr.PlayerGui["HUD"].GearHotbar:WaitForChild("HotbarFrame")

local function updatehotbar()
	for i,v in pairs(hotbarSlots:GetChildren()) do
		hotbar[v].SlotIcon.Image = "rbxassetid://" .. v.Icon.Value
		hotbar[v].CurrentSlot = v.Value
	end
end

for i,v in pairs(hotbarSlots:GetChildren()) do
	v.Changed:Connect(updatehotbar)
	v.Icon.Changed:Connect(updatehotbar)
end

is v.Icon.Value the actual value or is it the instance that holds the value?
Also try using tostring() on the value

Icon is a string value, i already tried using tostring() but it still didnt work

well here’s a discrepancy

image

What is v really?

v is like, the variable on for i,v in pairs that i did, hotbarSlots is a folder that has the values of each hotbar slots, like, there are 5 values in it named from 1 to 5 which are the same names as the hotbar slots in the UI, idk if u could understand but i can try to explain it better

can you show a screenshot of one of the “v” so we can see what children it has (in the explorer)

Alright, here it is, all the values inside the instances should be the same pretty much

The hotbar values, on number 1 it should be primary but it says heal, ima fix it later
image

And here is the ui part
image

try replacing both hotbar[v]

with hotbar[tostring(i)] since V is the actual instance and I is the index. Alternativ add .Name
so replace them with:

hotbar[v.Name]
--or do this
hotbar[tostring(i)]

Alright, its pretty late in my country so i will test it tomorrow and see if it works

1 Like

Just tested it. This worked, thanks!

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