Pogo Stick Code

Hello! I recently made a pogo stick script and even though everything works fine, I noticed how cluttered/unoptimized my code was. I know that I need to use module scripts and things to OOP but is there anything else to do(so that I can optimize my code)? Please let me know!

Server:

--         SERVICES         --
local debris_S = game:GetService("Debris")

--			ITEMS			--
local remotesFldr = script.Parent.Parent:WaitForChild("Remotes")
local pogoBase = script.Parent.Parent
local pPrompt = pogoBase.PPrompt
local landinfEff = pogoBase.Parent:WaitForChild("Stick"):WaitForChild("LandingEffect")
local anim = pogoBase.Parent:WaitForChild("Animations"):WaitForChild("PogoIdle")

--			FUNCTIONS			--
function addEffect(plr,num)
	landinfEff:Emit(num)
end

function playSnd(plr,SoundName)
	script.Parent.Parent.Sounds:FindFirstChild(SoundName):Play()
end


--			PROXIMITY PROMPT			--
pPrompt.Triggered:Connect(function(plr)
	pogoBase.Parent:SetAttribute("Equipped",true)
	pPrompt.Enabled = false
	
	local char = plr.Character
	local hum = char:WaitForChild("Humanoid")
	local humRoot = char:WaitForChild("HumanoidRootPart")
	
	
	local wc = Instance.new("RigidConstraint",pogoBase)
	local baseM6D = Instance.new("Motor6D",humRoot)
	
	pogoBase.Parent.Parent = char
	
	baseM6D.Part0 = humRoot
	baseM6D.part1 = pogoBase
	wc.Attachment0 =pogoBase.BodyAttachment
	wc.Attachment1 = char:WaitForChild("HumanoidRootPart").RootRigAttachment

	pogoBase.Anchored = false
	pogoBase.Parent.Stick.Anchored = false
	
	
	remotesFldr:WaitForChild("Jump"):FireClient(plr,hum,humRoot.Position)
	remotesFldr:WaitForChild('RemovePlayer').OnServerEvent:Connect(function(plr)
		wc:Destroy()
		baseM6D:Destroy()
		
		pogoBase.Parent:SetAttribute("Equipped",false)
		
		pPrompt.Enabled = true
		pogoBase.Parent.Parent = game.Workspace
	end)
	remotesFldr:WaitForChild("AddEffect").OnServerEvent:Connect(addEffect)
	remotesFldr:WaitForChild("PlaySound").OnServerEvent:Connect(playSnd)
end)

Client:


--			SERVICES			--
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local DEBRIS = game:GetService("Debris")

--			REQUIRED OBJECTS			--
local cam = game.Workspace.CurrentCamera
local pogoModel = script.Parent.Parent.Parent
local remotesFldr = script.Parent.Parent:WaitForChild("Remotes")
local idleAnim = pogoModel.Animations:WaitForChild("PogoIdle")
local powerJumpAnim = pogoModel.Animations:WaitForChild("PowerPogoJump")
local hum = game.Players.LocalPlayer.Character:WaitForChild('Humanoid')
local humRoot = game.Players.LocalPlayer.Character:WaitForChild('HumanoidRootPart')

--			BOOLS-CONNECTIONS-SETUP			--
local CLOSE_TO_GROUND = false
local CAN_RAYCAST,CAN_JUMP,HAS_JUMPED,CAN_EXIT = false,true,false,false 
local JUMP_CONNECTION, KEYFRAME_CONNECTION

local tInfo = TweenInfo.new(0.37,Enum.EasingStyle.Quad,Enum.EasingDirection.Out)

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {script.Parent.Parent, pogoModel.Stick}



--			FUNCTIONS			--
local function JumpDebounce() --Adding d
	CAN_JUMP = false
	delay(5,function()
		CAN_JUMP = true
	end)
end

local function Setup(hum)
	CAN_EXIT = false
	HAS_JUMPED = false
	
	hum:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
	hum:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)

	hum.WalkSpeed = 10
	
	delay(1,function()
		if HAS_JUMPED == false then
			CAN_EXIT = true
		end
	end)
	
end

local function NormalGroundingEvents(hum) --Normal Jump Events
	task.wait(0.17)
	remotesFldr:WaitForChild("AddEffect"):FireServer(3)
	remotesFldr:WaitForChild("PlaySound"):FireServer("Boing")
	hum:ChangeState(Enum.HumanoidStateType.Jumping)
end

local function PowerGroundingEvents(hum) --Power Jump Events
	if not pogoModel.PogoBase:FindFirstChild("UpForce") then
		
		remotesFldr:WaitForChild("PlaySound"):FireServer("PowerJumpSnd")
		hum:ChangeState(Enum.HumanoidStateType.Jumping) 
		
		local Vforce = Instance.new("VectorForce",script.Parent.Parent)
		Vforce.Name = "UpForce"
		Vforce.Attachment0 = script.Parent.Parent.BodyAttachment
		Vforce.Force = Vector3.new(0,7777,0)
		
		DEBRIS:AddItem(Vforce,0.3)
	end
end

local function WidenFov()
	local tweenWide = TS:Create(cam,tInfo,{FieldOfView = 90})
	local tweenIn = TS:Create(cam,tInfo,{FieldOfView = 70})
	
	local FovWidener = coroutine.create(function()
		tweenWide:Play()
		repeat wait() until CLOSE_TO_GROUND == true
		tweenIn:Play()
	end)
	
	coroutine.resume(FovWidener)
end

local function RaycastDistance() --Checking Distance to avoid Flinging
	if CAN_RAYCAST == true then
		local stick  = pogoModel.Stick
		local rotation = CFrame.Angles(math.rad(-90),0,0)
		
		local origin = stick.Position
		local dir = (stick.CFrame * rotation).LookVector * 17
	
	
		local raycasted = workspace:Raycast(origin,dir,params)
		
		if raycasted then
			if raycasted.Distance < 7 then
				CLOSE_TO_GROUND = true
			end
		end
	end
end

local function AddLandingForce() --Adding a landing force before player hits the ground to Avoid Flinging
	remotesFldr:WaitForChild("PlaySound"):FireServer("PowerJumpLand")
	remotesFldr:WaitForChild("AddEffect"):FireServer(17)
	hum:ChangeState(Enum.HumanoidStateType.Jumping) 
	
	local Vforce = Instance.new("VectorForce",script.Parent.Parent)
	Vforce.Attachment0 = script.Parent.Parent.BodyAttachment
	Vforce.Force = Vector3.new(0,workspace.Gravity * (script.Parent.Parent:GetMass()/2),0)


	DEBRIS:AddItem(Vforce,0.2)
end

local function MAINJumpEvent(hum,humrootPos)
	
	Setup(hum)
	
	local pogoIdle = hum:LoadAnimation(idleAnim)
	local pogoPowerJump = hum:LoadAnimation(powerJumpAnim)
	
	pogoIdle:Play()
	
	
	KEYFRAME_CONNECTION = pogoIdle.KeyframeReached:Connect(function(Grounded)
		NormalGroundingEvents(hum)
	end)
	
	JUMP_CONNECTION = UIS.JumpRequest:Connect(function()
		if CAN_JUMP == true then
			-- Setting bools to avoid errors
			HAS_JUMPED = true
			CAN_EXIT = false
			CLOSE_TO_GROUND = false
			
			-- Stoping current animations and starting the Power Jump anim
			JumpDebounce()
			pogoIdle:Stop()
			hum:ChangeState(Enum.HumanoidStateType.Jumping)
			pogoPowerJump:Play()
			
			--Adding a force to make the player jump high, Starts raycasting distance
			PowerGroundingEvents(hum)
			WidenFov()
			task.wait(0.25)
			CAN_RAYCAST = true
			
			--Waits until the player is close to the ground then adds a force to Stop Flinging
			repeat wait() until CLOSE_TO_GROUND == true
			AddLandingForce()
			task.wait(0.25)
			
			--Stops Power Jump anim and stops Raycasting Distance
			pogoPowerJump:Stop()
			pogoIdle:Play()
			CAN_RAYCAST = false
			
			--Adds a wait before exit to stop Errors
			task.wait(1)
			CAN_EXIT = true	
		end
	end)
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.E and CAN_EXIT == true then
			remotesFldr:WaitForChild('RemovePlayer'):FireServer()
			hum.WalkSpeed = 16
			
			local animSuccess, animError = pcall(function()
				pogoIdle:Stop()
				pogoPowerJump:Stop()
			end)
			if animError then
				pogoIdle:Stop()
				pogoPowerJump:Stop()
				warn("TRYING AGAIN!")
			end
			
			JUMP_CONNECTION:Disconnect()
			KEYFRAME_CONNECTION:Disconnect() 
		
		end
	end)


end



--			EVENTS/RUNSERVICE			--
remotesFldr.Jump.OnClientEvent:Connect(function(hum : Humanoid, humRootPos : Vector3)
	local equipped = pogoModel:GetAttribute("Equipped")
	
	if equipped == true then
		MAINJumpEvent(hum,humRootPos)
	end
	
end)

game:GetService("RunService").RenderStepped:Connect(RaycastDistance)

Constructive criticisms please!

1 Like

I suggest to make some more variables for items that you are calling many times or also just once such as services.

1 Like