So I have a script where you can sort between 8 cameras and I have it set up so that if the index is 1 and you press the previous button, it sends you to 8. If the index is 8 and you press next it sends you to 1 but for some reason this is not working correctly, it sends me to a random number and disrupts the whole order until you land back at 1. I was wondering if this can be fixed in any way and how I would go about fixing it.
#Index not working properly when I go under or over min/max amt
1 messages · Page 1 of 1 (latest)
here is the button logic:
next.MouseButton1Click:Connect(function()
currentindex = currentindex + 1
if currentindex > #cameraParts then
currentindex = 1
end
setCamera(currentindex)
end)
previous.MouseButton1Click:Connect(function()
currentindex = currentindex - 1
if currentindex < 1 then
currentindex = #cameraParts
end
setCamera(currentindex)
end)
I can send how the table is sorted if necessary as well
Send it
Why not
Also set camera
The important thing isn't this part of the code but probably your setCamera function.
Alright give me a few hours mb
K
function setCamera(index)
local camPart = cameraParts[index]
if camPart and camPart:IsA("BasePart") and camPart.CFrame then
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = cameraParts[currentindex].CFrame
end
end
Here it is
Just to test it out, put camera.CFrame in a loop
Like this:
local RunService = game:GetService("RunService")
local forceCamera = true
function setCamera(index)
local camPart = cameraParts[index]
if camPart and camPart:IsA("BasePart") and camPart.CFrame then
camera.CameraType = Enum.CameraType.Scriptable
currentCamera = camPart
end
end
RunService.Heartbeat:Connect(function()
if forceCamera and currentCamera then
camera.CFrame = currentCamera.CFrame
end
end)
Alright one sec
yo wait what is currentCamera i dont have it defined, i only have camera defined
rq
Lmk if i did anything wrong here
What you sent is not giving me any info on what you did wrong, you can either send me the full code, or try to explain exactly what you want to achieve so I can send you a script that does it.
Oh alr one sec
local cameraRoom = workspace:WaitForChild("CameraRoom")
local cameraFolder = cameraRoom:WaitForChild("Cams")
local cameraParts = cameraFolder:GetChildren()
table.sort(cameraParts, function(a,b)
return tonumber(a.Name) < tonumber(b.Name)
end)
repeat wait() until #cameraFolder:GetChildren() > 0
for _, obj in pairs(cameraFolder:GetChildren()) do
if obj:IsA("BasePart") then
table.insert(cameraParts,obj)
end
end
local currentindex = 1
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local gui = script.Parent
local previous = gui:WaitForChild("Prev")
local next = gui:WaitForChild("Next")
function setCamera(index)
local camPart = cameraParts[index]
if camPart and camPart:IsA("BasePart") and camPart.CFrame then
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = cameraParts[currentindex].CFrame
end
end
next.MouseButton1Click:Connect(function()
currentindex = currentindex + 1
if currentindex > #cameraParts then
currentindex = 1
end
setCamera(currentindex)
end)
previous.MouseButton1Click:Connect(function()
currentindex = currentindex - 1
if currentindex < 1 then
currentindex = #cameraParts
end
setCamera(currentindex)
end)
game.ReplicatedStorage:WaitForChild("EnterCam").OnClientEvent:Connect(function()
gui.Enabled = true
currentindex = 1
setCamera(currentindex)
end)
-- Variables:
local cameraRoom = workspace:WaitForChild("CameraRoom")
local cameraFolder = cameraRoom:WaitForChild("Cams")
local cameraParts = {}
for _, child in cameraFolder:GetChildren() do
cameraParts[tonumber(child.Name)] = child
end
local currentIndex = 1
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local gui = script.Parent
local previousButton = gui:WaitForChild("Prev")
local nextButton = gui:WaitForChild("Next")
-- Functions:
function setCamera(index)
local camPart = cameraParts[index]
if camPart and camPart:IsA("BasePart") and camPart.CFrame then
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = camPart.CFrame
end
end
-- Connections:
nextButton.MouseButton1Click:Connect(function()
currentIndex = ((currentIndex < #cameraParts) and currentIndex + 1) or 1
setCamera(currentIndex)
end)
previousButton.MouseButton1Click:Connect(function()
currentIndex = ((currentIndex > 1) and currentIndex - 1) or #cameraParts
setCamera(currentIndex)
end)
game.ReplicatedStorage:WaitForChild("EnterCam").OnClientEvent:Connect(function()
gui.Enabled = true
currentIndex = 1
setCamera(currentIndex)
end)
cameraFolder.ChildAdded:Connect(function(child)
cameraParts[tonumber(child.Name)] = child
end)
Test if that works
Alright
** You are now Level 3! **
It did thank you so much bro