like this
print(table.find(self.GunAnimations, "Equip"))
self.GunAnimations["Equip"]
these return nil; I’m trying to get the value named Equip in the table, how could i do this
this is the table
{ [1] = Equip }
like this
print(table.find(self.GunAnimations, "Equip"))
self.GunAnimations["Equip"]
these return nil; I’m trying to get the value named Equip in the table, how could i do this
this is the table
{ [1] = Equip }
Make it a dictionary in the first place
instead of
[1] = Equip;
do
Equip = {…};
That way you can access the data by just doing Table[“Equip”]
is there a way to insert values into a dictionary?
for index, AnimationObject in pairs(self.Gun.Folders.Animations:GetChildren()) do
if AnimationObject:IsA("Animation") then table.insert(self.GunAnimations ,self.Player.Character.Humanoid.Animator:LoadAnimation(AnimationObject))
print("Loaded", AnimationObject)
end
end
In order to effectively use table.find()
, you need reference to the value you are trying to find. From the looks of it, you are trying to find an animation and you only know the name. This animation you are looking for is in an array (special kind of dictionary). To find it, you can iterate through the array and compare the animation’s name to “Equip”.
index = nil
for i, v in ipairs(self.GunAnimations) do
if v.Name == "Equip" then
index = i
end
end
return index
I’m assuming you are storing the actual animation objects in your table and not strings that represent their names. If so, table.find
isn’t working because you’re trying to find a string value when what you’re trying to get is an object value. It will also return an index, not the value. Your second method also won’t work because you don’t have a key
with the name “Equip”.
You could try having your table organized where it would read {["Equip"] = animationObject}
, that way in order to find it you can just do tableName[key]
to find it.
Ignore the animations being in Workspace
, just using it as an example.
You can do this by doing this: myTable[key] = value
. For example (again ignore the animations being in Workspace
, just an example):
Array’s aren’t special types of dictionaries. Dictionaries are formally known as hash maps, and this data structure is built on arrays. An array in this context is a specific configuration of a Luau table, which itself is a hybrid data structure of an actual array and hash map.
Additionally, there is not need to continue iterating through the GunAnimations
table if the value has been found. Add a break
after you set the index. Regardless, I’m pretty sure OP want’s the value, not the index. You can take advantage of your existing logic and use v
over i
. Ultimately, it’s best you write a generalized function for making this search:
local function getAnimationTrackByName(animations: {AnimationTrack}, name: string): AnimationTrack
for _, animationTrack in animationTracks do
if animationTrack.Name == name then
return animationTrack
end
end
return nil
end
local equipAnimation = getAnimationTrackByName(self.GunAnimations, "Equip")
thank you this is what i needed
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.