I have this script that’ll manage different “modes” a player can cycle through using Q and R. Q will move back and R will move forwards. The table has the name of all the modes while the order will indicate the order in which the player can switch to the next / previous mode.
How would I have the script know the current mode it’s on and move to the next / previous one if possible? I know that Table.Next exists but is there an inverse to this or is there a better way to approach this as a whole? Thanks for any help.
plr = game.Players.LocalPlayer
char = plr.Character or plr.CharacterAdded:Wait()
Sword = script.Parent.RiyetenSword
UIS = game:GetService("UserInputService")
Mode = "None"
--Modes
Modes = {
C1 = {Name = "Dreamcaster", Order = 1},
C2 = {Name = "Fireclaw", Order = 2},
C3 = {Name = "Guardian", Order = 3},
C4 = {Name = "The Manual", Order = 4},
C5 = {Name = "Console", Order = 5},
}
Sword.CanCollide = false
Sword.Parent = char
SW = Instance.new("WeldConstraint")
SW.Parent = char
SW.Part0 = char.Torso
SW.Part1 = Sword
Sword.Position = char.Torso.Position - Vector3.new(0,0,-0.5)
UIS.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.Q then
-- Stuff will happen here
end
if Key.KeyCode == Enum.KeyCode.R then
-- Stuff will happen here
end
end)
I believe that this works after implementing it, thank you both for your awnsers! One thing though, How would I print out the name of the current mode since “Name” is no longer a variable (atleast how I see it.)
Sorry to be a bother again, but after i’ve added the final stuff I’ve been trying to fix this issue when trying to print the names and I haven’t figured out why its happening.
Trying to print returns with nil
UIS.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.E then
local Final = IterateModes(Modes, CurrentMode.Value, true)
print(Final["Name"])
end
if Key.KeyCode == Enum.KeyCode.Q then
local Final = IterateModes(Modes, CurrentMode.Value, false)
print(Final["Name"])
end
end)
I basically tried to implement the answer provided but it has not worked. I’m assuming I need to return something but i’m unsure.
The cycle still works, but the script doesn’t know what the index is referring to (?)
Based on what you are saying that the script does not know what the index is referring to, that means the issue is probably coming from:
What have you set this to mean (as far as I can see, in any of the code snippets, CurrentMode was never defined nor is it clear that it has a Value index)? For the IterateTable/IterateMode function, the second argument should be a number, representing the current index in the array. The function is then set to return something.
Could you post the complete code that you are working with right now?
Ah, No worries, I resolved the issue by using a few values here and there, and works as intended, I’m still a bit wobbly on using tables and messed up somewhere. Thank you for clarifying, I’ll try to be more open minded before posting questions. Thanks and have a happy new year