I am making a system where when you press a button it takes you to a world but it cycles through it and its not working
local player = game.Players.LocalPlayer local char = player.Character local rep = game:GetService("ReplicatedStorage") local event = rep:WaitForChild("WorldChange", 5) local allchar = char:GetChildren() local serverstorage = game:GetService("Workspace") local tele = serverstorage:WaitForChild("Tele") function MainWorld() script.Parent.Current.Text = "Main World" script.Parent.Last.Text = "shadow World" script.Parent.Next.Text = "hidden World" script.Parent.Left.MouseButton1Click:Connect(function() ShadowWorld() end) script.Parent.Right.MouseButton1Click:Connect(function() HiddenWorld() end) if tele then char.LowerTorso.CFrame = CFrame.new(tele.Position) else print("its nil") end end function ShadowWorld() script.Parent.Current.Text = "Shadow World" script.Parent.Last.Text = "hidden World" script.Parent.Next.Text = "main World" script.Parent.Left.MouseButton1Click:Connect(function() HiddenWorld() end) script.Parent.Right.MouseButton1Click:Connect(function() MainWorld() end) end function HiddenWorld() script.Parent.Current.Text = "Hidden World" script.Parent.Last.Text = "main World" script.Parent.Next.Text = "shadow World" script.Parent.Left.MouseButton1Click:Connect(function() MainWorld() end) script.Parent.Right.MouseButton1Click:Connect(function() ShadowWorld() end) end MainWorld()
i am not getting any errors and the things that are happening are,
when i press the button to go to the hidden world, it works but after that if i try to go to the shadow world it does not work and runs the code for the main world, and if i press main then it does the shadow world and normally crashes my game
please help i cant fix it
ok you have way more code than you need, try not using too much, also, its quite simple to cycle through anything, make a variable and add to that variable each time it cycles, and then have a maximum for that variable, if it reaches that maximum number, it resets.
world = 0 --0-2 because there are 3 worlds debounce = true--just in case script.Parent.Left.MouseButton1Click:Connect(function() if world == 0 and debounce == true then debounce = false MainWorld() world += 1 debounce = true elseif world == 1 and debounce == true then debounce = false ShadowWorld() world += 1 debounce = true elseif world == 2 and debounce == true then debounce = false HiddenWorld() world = 0 debounce = true end end)
you can make this even shorter by instead of making a different elseif each imte, just making a string array and looping through each one, the first one being "MainWorld", second being "ShadowWorld" and so on, then just call it kind of this: (array[world])(), i dont know if thats exaclty how but yeah do that.
Or just rename the worlds to 1,2,3 and boom just call it like that:
[world]()--world is anumebr so it calls that function because its named after that number
hope this helps :3