So i “Managed” to make it work properly the normal way, just spectating “forward” but when it comes to returning to the previous player i don’t know how to do it.
This is my script, i simply tried changing the “+” signals to “-” ones, but it makes the previous button work 2 or 3 times before breaking:
UserInputService.InputBegan:connect(function(inputObject)
if inputObject.KeyCode == Enum.KeyCode.Q and script.Parent.Visible == true then
script.Parent:TweenPosition(UDim2.new(0.31, 0,0.5, 0), "In", "Linear", 0.1, true)
on=0
local plrs = workspace.Ingame:GetChildren()
num=num+1
if num ==#plrs +1 then
num = 1
end
for i =1,#plrs do
on=on+1
if on == num then
cam.CameraSubject = plrs[i]
wait(0.1)
script.Parent:TweenPosition(UDim2.new(0.3, 0,0.5, 0), "In", "Linear", 0.1, true)
end
end
end
end)
For loops is one of the most things that give me a headache when i try to understand how it works and it’s logic, if anyone could help and explain to me how to do it properly i would be very grateful
Oh i managed to fix it with your advice, but i still don’t understand how this works or the logic behind it, i need to get into more For Loops to get a better insight.
Previously, you had a table of players to spectate and a number variable for which index of that table you were currently spectating. When going forwards, the number went up by 1, indicating that you are moving onto the next player in the table. However, by subtracting, you are going backwards.
The if statement is to prevent the num variable from being an invalid table key. For example, in a table that looks like this, {Player1, Player2, Player3} with a num variable of 2, you would be viewing Player2 because they are the 2nd player in the table. If you subtract once, you go to the first player in the table. However, if you subtract again, an error would occur because the table has no 0th value. Therefore, you would wrap around to the last player in the table, or Player3 in the example.
The same thinking applies in both directions. I hope this clarifies.
Not gonna lie, i’ve been trying to understand For Loops since when i started learning lua (about a month ago?) and i still have a hard time trying to understand all those Index stuff, but yes, your explanation gave me a better insight on how the loop is working with the spectate GUI.
I will go one step further as I want to make sure you understand for the future.
Tables are a very important part of Lua and any programming language, although right now, I’ll keep it to Lua.
In Lua, tables are formatted with a key and then value. In a table like this, local t = {a, b, c}, a would have a key of 1 because it is first, b would have a key of 2, etc. You can’t see these keys directly, but they exist.
If, using the same table, I did t[2], I would get b, because b is the value tied to the key of 2.
In your spectate script, you have a list of players. Each player has a key, or number associated with them in the table. Your num variable represents the key of the value you are currently spectating. When you change your num variable you change the CameraSubject to something like plrs[num]....
I highly recommend you read this article to learn more about tables and key/index, value pairs.
I’ll check this article for sure, you really gave me a better understanding on this stuff and i hope i can learn them as soon as possible so i don’t have to struggle with it anymore (atleast with simple stuff like this xD). Thanks for all your help and patience, i really appreciate it