(probably) basic scripting help

I would like to start this post by saying that to anyone who can take the time to read through my code and help me, I will forever be grateful. :pray:

Hi. I am making a game where two players step on pads and get sent to play against each other. I have written this script: (hopefully my code is clear enough)

Script inside the entrance thing
local players = game:GetService("Players")
local replicated = game:GetService("ReplicatedStorage")
local ts = game:GetService("TweenService")

local enterEvent = replicated:WaitForChild("PlayerEntered")
local exitEvent = replicated:WaitForChild("PlayerExit")

local winEvent = replicated:WaitForChild("WinLoseEvents"):WaitForChild("WinEvent")
local loseEvent = replicated:WaitForChild("WinLoseEvents"):WaitForChild("LoseEvent")

local blocker = script.Parent.Blocker
local blockerStart = script.Parent.BlockerStartPos
local blockerEnd = script.Parent.BlockerEndPos

local pad1 = script.Parent.Pad1
local pad2 = script.Parent.Pad2

local gameTP = script.Parent.GameTP

local pad1Frame = script.Parent.UIPart.SurfaceGui.Frame.Pad1Frame
local pad2Frame = script.Parent.UIPart.SurfaceGui.Frame.Pad2Frame

local pad1Icon = pad1Frame.ImageLabel
local pad2Icon = pad2Frame.ImageLabel

local pad1PlayerDisplay = pad1Frame.PlayerDisplay
local pad2PlayerDisplay = pad2Frame.PlayerDisplay

local pad1PlayerName = pad1Frame.PlayerName
local pad2PlayerName = pad2Frame.PlayerName

local pad1PlayerAmount = pad1Frame.PlayerAmount
local pad2PlayerAmount = pad2Frame.PlayerAmount

local campart = script.Parent.CamPos

local tpPart = script.Parent.TPPart

local endPart = script.Parent.EndPart

local pad1Player = nil
local pad2Player = nil

local ongoing = false

pad1.Material = Enum.Material.SmoothPlastic
pad2.Material = Enum.Material.SmoothPlastic

pad1PlayerAmount.Text = "0/1"
pad2PlayerAmount.Text = "0/1"

pad1PlayerDisplay.Text = ""
pad2PlayerDisplay.Text = ""

pad1PlayerName.Text = ""
pad2PlayerName.Text = ""


local tweeninfClose = TweenInfo.new(2, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out, 0, false, 0)
local tweeninfOpen = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)

local tweenClose = ts:Create(blocker, tweeninfClose, {CFrame = blockerEnd.CFrame})
local tweenOpen = ts:Create(blocker, tweeninfOpen, {CFrame = blockerStart.CFrame})

local defaultIcon = "rbxassetid://0"
pad1Icon.Image = defaultIcon
pad2Icon.Image = defaultIcon

local function reset(pad)

	if pad == pad1 then
		
		if game.Players:FindFirstChild(pad1Player) then
			pad1Player.Character:WaitForChild("HumanoidRootPart").CFrame = tpPart.CFrame
			exitEvent:FireClient(pad1Player)
		end
		
		pad1Player = nil
		
		pad1PlayerAmount.Text = "0/1"
		pad1PlayerDisplay.Text = ""
		pad1PlayerName.Text = ""
		pad1Icon.Image = defaultIcon
		pad1.Material = Enum.Material.SmoothPlastic
		pad1.BrickColor = BrickColor.new("Dark stone grey")
	elseif pad == pad2 then
		
		if game.Players:FindFirstChild(pad2Player) then
			pad2Player.Character:WaitForChild("HumanoidRootPart").CFrame = tpPart.CFrame
			exitEvent:FireClient(pad2Player)
		end
		
		pad2Player = nil
		
		pad2PlayerAmount.Text = "0/1"
		pad2PlayerDisplay.Text = ""
		pad2PlayerName.Text = ""
		pad2Icon.Image = defaultIcon
		pad2.Material = Enum.Material.SmoothPlastic
		pad2.BrickColor = BrickColor.new("Dark stone grey")
	end
end

local function start(player1, player2)
	print(player1)
	print(player2)
	
	ongoing = true
	
	tweenClose:Play()
	
	exitEvent:FireClient(player1)
	exitEvent:FireClient(player2)
	
	player1.Character:WaitForChild("HumanoidRootPart").CFrame = gameTP.CFrame
	player2.Character:WaitForChild("HumanoidRootPart").CFrame = gameTP.CFrame
end

players.PlayerRemoving:Connect(function(leavingPlayer)
	if ongoing then
		ongoing = false
		reset(pad1)
		reset(pad2)
		
		tweenOpen:Play()
	end
end)

endPart.Touched:Connect(function(hit)
	local char = hit.Parent

	if char:FindFirstChild("Humanoid") and ongoing then
		ongoing = false

		local player = players:GetPlayerFromCharacter(char)

		if player == pad1Player then
			winEvent:FireClient(pad1Player)
			loseEvent:FireClient(pad2Player)
		elseif player == pad2Player then
			winEvent:FireClient(pad2Player)
			loseEvent:FireClient(pad1Player)
		end

		reset(pad1)
		reset(pad2)

		tweenOpen:Play()
	end
end)

exitEvent.OnServerEvent:Connect(function(player)
	if player == pad1Player then
		reset(pad1)
	elseif player == pad2Player then
		reset(pad2)
	end
end)

local function pad1Touch(hit)
	local char = hit.Parent
	if pad1Player == nil then
		if char:FindFirstChild("HumanoidRootPart") then
			pad1Player = players:GetPlayerFromCharacter(char)

			enterEvent:FireClient(pad1Player, campart)
			
			pad1.BrickColor = BrickColor.new("New Yeller")
			pad1.Material = Enum.Material.Neon

			pad1PlayerAmount.Text = "1/1"
			pad1PlayerDisplay.Text = pad1Player.DisplayName
			pad1PlayerName.Text = "@"..pad1Player.Name
			pad1Icon.Image = players:GetUserThumbnailAsync(pad1Player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
			
			if pad1Player and pad2Player then
				start(pad1Player, pad2Player)
			end
		end
	end
end

local function pad2Touch(hit)
	local char = hit.Parent
	
	if pad2Player == nil then
		if char:FindFirstChild("HumanoidRootPart") then
			pad2Player = players:GetPlayerFromCharacter(char)

			enterEvent:FireClient(pad2Player, campart)
			
			pad2.BrickColor = BrickColor.new("New Yeller")
			pad2.Material = Enum.Material.Neon
			
			pad2PlayerAmount.Text = "1/1"
			pad2PlayerDisplay.Text = pad2Player.DisplayName
			pad2PlayerName.Text = "@"..pad2Player.Name
			pad2Icon.Image = players:GetUserThumbnailAsync(pad2Player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
			

			if pad1Player and pad2Player then
				start(pad1Player, pad2Player)
			end
		end
	end
end

pad1.Touched:Connect(pad1Touch)
pad2.Touched:Connect(pad2Touch)
LocalScript if needed
local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()

local replicated = game:GetService("ReplicatedStorage")
local ts = game:GetService("TweenService")

local startergui = script.Parent

local enterEvent = replicated:WaitForChild("PlayerEntered")
local exitEvent = replicated:WaitForChild("PlayerExit")

local winEvent = replicated:WaitForChild("WinLoseEvents"):WaitForChild("WinEvent")
local loseEvent = replicated:WaitForChild("WinLoseEvents"):WaitForChild("LoseEvent")

local WinLoseFramesOrigin = startergui.WinScreen.Canvas.Main.Position
local WinLoseFramesVisiblePos = UDim2.new(0.5,0,0.5,0)

local camera = game.Workspace.CurrentCamera

local UItweeninf = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)

local tweenWinOpen = ts:Create(startergui.WinScreen.Canvas.Main, UItweeninf, {Position = WinLoseFramesVisiblePos})
local tweenWinClose = ts:Create(startergui.WinScreen.Canvas.Main, UItweeninf, {Position = WinLoseFramesOrigin})

local tweenLoseOpen = ts:Create(startergui.LoseScreen.Canvas.Main, UItweeninf, {Position = WinLoseFramesVisiblePos})
local tweenLoseClose = ts:Create(startergui.LoseScreen.Canvas.Main, UItweeninf, {Position = WinLoseFramesOrigin})

enterEvent.OnClientEvent:Connect(function(campart)
	startergui.ExitGui.Enabled = true
	
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = campart.CFrame
	
	Controls:Disable()
end)

exitEvent.OnClientEvent:Connect(function()
	startergui.ExitGui.Enabled = false
	
	camera.CameraType = Enum.CameraType.Custom
	Controls:Enable()
end)

startergui.ExitGui.Exit.MouseButton1Click:Connect(function()
	exitEvent:FireServer()
end)

-- LOSE OR WIN

winEvent.OnClientEvent:Connect(function()
	startergui.WinScreen.Enabled = true
	tweenWinOpen:Play()
end)

loseEvent.OnClientEvent:Connect(function()
	startergui.LoseScreen.Enabled = true
	tweenLoseOpen:Play()
end)

startergui.WinScreen.Canvas.Main.Exit.MouseButton1Click:Connect(function()
	tweenWinClose:Play()
	task.wait(0.5)
	startergui.WinScreen.Enabled = false
end)

startergui.LoseScreen.Canvas.Main.Exit.MouseButton1Click:Connect(function()
	tweenLoseClose:Play()
	task.wait(0.5)
	startergui.LoseScreen.Enabled = false
end)

To start with, everything was working. I discovered a bug where if a player left after the round was over, the other player would be teleported (this is supposed to happen when the player is still dueling) but a simple bug turned into a rabbit hole of errors and bugs. Now, players can’t press the ‘exit’ button to be teleported and the player’s character isn’t setting to the camera part and idk where to even look in the code anymore. I have only made things worse :sob: :sob:

What are some of the errors you’re seeing?

1 Like

Hi! Sorry for the late response. I am highly grateful that you took the time to try and help. I, fortunately, have fixed the code.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.