Help fix bugs in shop code

I often have problems putting together code when it has something to do with teleporting (one spot to another spot in same game, not place to place) and a loading screen.

I usually fire the loading screen event and then place a wait() before the teleport code, but it isn’t always consistent and sometimes it teleports after the loading screen fades away. So I was wondering if there was a way to remove the wait() entirely for something much more consistent.

I tried using another event that would fire during the middle of the loading scene, but it is still inconsistent, especially when manipulating the camera as well.

Could you share the code used for both the loading screen and the teleportation? Using “wait()”

should not be inconsistent.

1 Like

Giving us the code or a part of it would be helpful, even some pseudocode.

However, wrapping either component in a task.spawn() should work. If you know how long your teleport screen takes to fade in and out then just wait for the screen to finish fading in, then teleport the player, then fade the screen out. I am assuming you are doing some cross boundry stuff, remote events, etc so heres the structure I’d use.

Server: Gets command to teleport the player.
Server: Fires a remote function to the player
Client: Recieves remote function invocation, fades the teleport screen in, returns true after the screen is faded
Server: Gets the return, teleports the player
Server: Fires remote event to player to hide teleport screen
Client: Recieves event, fades teleport screen out

I ended up deciding to remake the code due to it being unorganized and sloppy, and I’m planning on just implementing the loading screen code into it, but this would still be very helpful to know for the future.

I guess an example of the code would be

ReplicatedStorage.TransitionEvent:FireServer()
wait(1)
hrp.CFrame = tp.CFrame
Camera.CameraType = "Custom"

is TransitionEvent a remoteEvent or remoteFunction?

Remote event

don’t mind this

I remade, like, all of the code and there is still a delay when switching the cameras.

-- Services --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")


-- Player Elements --
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Camera = game.Workspace.CurrentCamera
local PlayerGui = Player.PlayerGui

local Humanoid = Character:FindFirstChild("Humanoid")
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")


-- UI Elements --
local shopFrame = script.Parent.Frame
local shopTitle = shopFrame.Title
local exitButton = shopFrame.Exit
local loadingFrame = PlayerGui.Transition.Frame


-- Workspace Elements --
local shopDoor = game.Workspace.LobbyRoom.ShopDoor

local shopCam = game.Workspace.Shup:WaitForChild("ShopCamera")

local tp1 = game.Workspace.Shup.ShopTP1 
local tp2 = game.Workspace.LobbyRoom.ShopTP2

local backgroundMusic = game.Workspace["Background Theme"]
local shopMusic = game.Workspace["Shop Theme"]


-- Lighting Elements --
local blur = Lighting.Blur


-- Tween Info --
local genInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0) -- Blur + Loading
local titleInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, true, 0) -- KnifeBrawl Title Sway + Cam Sway


-- Tweens --
local frameTweenOpen = TweenService:Create(loadingFrame, genInfo, {BackgroundTransparency = 0})
local frameTweenClose = TweenService:Create(loadingFrame, genInfo, {BackgroundTransparency = 1})

local blurtweenOpen = TweenService:Create(blur, genInfo, {Size = 24})
local blurtweenClose = TweenService:Create(blur, genInfo, {Size = 0})

local titleTweenRight = TweenService:Create(shopTitle, titleInfo, {Rotation = 2})
local titleTweenLeft = TweenService:Create(shopTitle, titleInfo, {Rotation = -2})

local camTiltRight
local camTiltLeft

-- Coroutines --
local titleCoroutine
local camCoroutine


-- Functions --
function openTransition(song, vol)
	local musicTween = TweenService:Create(song, genInfo, {Volume = vol})
	frameTweenOpen:Play()
	musicTween:Play()
	musicTween.Completed:Wait()
	song:Stop()
end

function closeTransition(song, vol)
	local musicTween = TweenService:Create(song, genInfo, {Volume = vol})
	song:Play()
	frameTweenClose:Play()
	musicTween:Play()
	musicTween.Completed:Wait()
end


local shopCamEnabled = false
local changeCam = false

function shopCamToggle()
	if shopCamEnabled == false then
		shopCamEnabled = true
		changeCam = false
		
		Camera.CameraType = "Scriptable"

		Camera.Changed:Connect(function()
			if Camera.CameraType ~= "Scriptable" and changeCam == false then
				Camera.CameraType = "Scriptable"
			end
		end)

		Camera.CFrame = shopCam.CFrame
		Camera.FieldOfView = 80
		
		camTiltRight = TweenService:Create(Camera, titleInfo, {CFrame = Camera.CFrame * CFrame.Angles(0,0,math.rad(1))})
		camTiltLeft = TweenService:Create(Camera, titleInfo, {CFrame = Camera.CFrame * CFrame.Angles(0,0,math.rad(-1))})
		
		camCoroutine = coroutine.create(function()
			while true do
				camTiltRight:Play()
				camTiltRight.Completed:Wait()
				camTiltLeft:Play()
				camTiltLeft.Completed:Wait()
			end
		end)
		
		coroutine.resume(camCoroutine)
		
	else
		changeCam = true
		Camera.CameraType = "Custom"
		Camera.FieldOfView = 100

		shopCamEnabled = false
		coroutine.close(camCoroutine)
	end
end




local debounce = false

shopDoor.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent).UserId == Player.UserId and debounce == false then
		debounce = true
		
		print(Humanoid)
		print(HumanoidRootPart)
		
		openTransition(backgroundMusic, 0)
		task.wait(0.5)
		
		HumanoidRootPart.CFrame = tp1.CFrame
		Humanoid.WalkSpeed = 0
		shopCamToggle()
		
		titleCoroutine = coroutine.create(function()
			while true do
				titleTweenRight:Play()
				titleTweenRight.Completed:Wait()
				titleTweenLeft:Play()
				titleTweenLeft.Completed:Wait()
			end
		end)
		
		coroutine.resume(titleCoroutine)
		
		task.wait(0.5)
		closeTransition(shopMusic, 0.5)
		
		blurtweenOpen:Play()
		blurtweenOpen.Completed:Wait()
		shopFrame:TweenPosition(UDim2.new(0.5, 0 ,0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 1.5, true)
		
		debounce = false
		
	end
end)

local exitDebounce = false

exitButton.MouseButton1Click:Connect(function()
	if exitDebounce == false then
		exitDebounce = true
		
		shopFrame:TweenPosition(UDim2.new(0.5, 0 ,1.5, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine, 1.5, true)
		blurtweenClose:Play()
		
		coroutine.close(titleCoroutine)
		
		openTransition(shopMusic, 0)
		task.wait(0.5)

		HumanoidRootPart.CFrame = tp2.CFrame
		Humanoid.WalkSpeed = 13

		shopCamEnabled = false
		shopCamToggle()

		task.wait(0.8)
		closeTransition(backgroundMusic, 0.5)
		
		exitDebounce = false
		
	end
end)

There is also a problem where the camera likes to tween between the entrance to the shop and the actual shop camera?