--[[
	New Zero-G Client
	Phoenix & BmanGames eeeeyyet
	Another beaut
]]

local ZeroG = game:GetService("ReplicatedStorage").Game.Events.Game.ZG

local UIS = game:GetService("UserInputService")
local StarterGui = game:GetService("StarterGui")

local Moving = false
local WDown  = false
local SDown  = false
local ADown  = false
local DDown  = false

local Signals = {}

local Disable = function(Lab)
	game:GetService("Workspace").Audios.Local.ZeroG:Stop()

	local Character = game:GetService("Players").LocalPlayer.Character
	
	for Index, Signal in pairs(Signals) do
		Signal:Disconnect()
		Signals[Index] = nil
	end

	local Humanoid = Character:WaitForChild("Humanoid")
	
	for Index, En in pairs(Enum.HumanoidStateType:GetEnumItems()) do
	    if En ~= Enum.HumanoidStateType.None then  
	        Humanoid:SetStateEnabled(En, true) 
	    end
	end

	for Index, Track in pairs(Humanoid:GetPlayingAnimationTracks()) do
		Track:Stop()
	end

	if Lab == true then StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true) end

	StarterGui:SetCore("ResetButtonCallback", true)
	Character.Animate.Disabled = false

	game:GetService("RunService").RenderStepped:Wait()
	
	Moving = false
	WDown  = false
	SDown  = false
	ADown  = false
	DDown  = false
end

local Active = function(Lab)
	game:GetService("Workspace").Audios.Local.ZeroG:Play()
	
	local Character = game:GetService("Players").LocalPlayer.Character
	
	Character:WaitForChild("Humanoid")

	if Lab == true then Character:WaitForChild("Humanoid"):UnequipTools() end
	
	local Run = Character.Humanoid:LoadAnimation(Character.Animate.run.RunAnim)
	local Swim = Character.Humanoid:LoadAnimation(Character.Animate.swim.Swim)
	local SwimIdle = Character.Humanoid:LoadAnimation(Character.Animate.swimidle.SwimIdle)

	Signals.Changed = Character:WaitForChild("Humanoid").Changed:connect(function()
		Character.Humanoid.Jump = false
	end)
	
	Signals.Swimming = Character.HumanoidRootPart.Swimming.Changed:connect(function()
		Character.HumanoidRootPart.Swimming:Stop()
	end)
	
	Signals.Splash = Character.HumanoidRootPart.Splash.Changed:connect(function()
		Character.HumanoidRootPart.Splash:Stop()
	end)
	
	if Lab == true then StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) end
	
	StarterGui:SetCore("ResetButtonCallback", false)
	Character.Animate.Disabled = true

	local Humanoid = Character:WaitForChild("Humanoid")
	
	for Index, Track in pairs(Humanoid:GetPlayingAnimationTracks()) do
		Track:Stop()
	end

	for Index, En in pairs(Enum.HumanoidStateType:GetEnumItems()) do
	    if En ~= Enum.HumanoidStateType.None then  
	        Humanoid:SetStateEnabled(En, false) 
	    end
	end

	Run:Stop()
	Swim:Stop()
	SwimIdle:Play()

	Humanoid:ChangeState(Enum.HumanoidStateType.Swimming)

	Signals.Began = UIS.InputBegan:Connect(function(Input)
		if Input.UserInputType == Enum.UserInputType.Keyboard then
			if Input.KeyCode == Enum.KeyCode.W then
				WDown = true
			elseif Input.KeyCode == Enum.KeyCode.S then
				SDown = true
			elseif Input.KeyCode == Enum.KeyCode.A then
				ADown = true
			elseif Input.KeyCode == Enum.KeyCode.D then
				DDown = true
			end
		end
	
		if Input.UserInputType == Enum.UserInputType.Gamepad1 then
			if Input.Delta.X > 0 then
				DDown = true
	 		elseif Input.Delta.X < 0 then
				ADown = true
			end
	
			if Input.Delta.Y > 0 then
				WDown = true
			elseif Input.Delta.Y < 0 then
				SDown = true
			end
		end
	
		if Input.UserInputType == Enum.UserInputType.Touch then
			if Input.Position.X > 0 then
				DDown = true
	 		elseif Input.Position.X < 0 then
				ADown = true
			end
			
			if Input.Position.Y > 0 then
				WDown = true
			elseif Input.Position.Y < 0 then
				SDown = true
			end
		end
	end)	
	
	Signals.Ended = UIS.InputEnded:Connect(function(Input)
		if Input.UserInputType == Enum.UserInputType.Keyboard then
			if Input.KeyCode == Enum.KeyCode.W then
				WDown = false
			elseif Input.KeyCode == Enum.KeyCode.S then
				SDown = false
			elseif Input.KeyCode == Enum.KeyCode.A then
				ADown = false
			elseif Input.KeyCode == Enum.KeyCode.D then
				DDown = false
			end
		end
	
		if Input.UserInputType == Enum.UserInputType.Gamepad1 then
			if Input.Delta.X > 0 then
				DDown = false
	 		elseif Input.Delta.X < 0 then
				ADown = false
			end
	
			if Input.Delta.Y > 0 then
				WDown = false
			elseif Input.Delta.Y < 0 then
				SDown = false
			end
		end
	
		if Input.UserInputType == Enum.UserInputType.Touch then
			if Input.Position.X > 0 then
				DDown = false
	 		elseif Input.Position.X < 0 then
				ADown = false
			end
			
			if Input.Position.Y > 0 then
				WDown = false
			elseif Input.Position.Y < 0 then
				SDown = false
			end
		end
	end)
	
	Signals.Stepped = game:GetService("RunService").RenderStepped:Connect(function()
		if WDown == true or SDown == true or ADown == true or DDown == true then
			if Moving == false then
				Moving = true
				SwimIdle:Stop()
				Swim:Play()
			end
		else
			if Moving == true then
				Moving = false
				Swim:Stop()
				SwimIdle:Play()
			end
		end
	end)
end



ZeroG.OnClientEvent:Connect(function(State, LabBool)
	if State == true then
		Active(LabBool)
	else
		Disable(LabBool)
	end
end)