Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

local Player = game.Players.LocalPlayer local Character = Player.Character local…

  1. User

    local Player = game.Players.LocalPlayer local Character = Player.Character local MainFrame = script.Parent local Camera = workspace.CurrentCamera local Humanoid = Character:WaitForChild("Humanoid") local Mouse = Player:GetMouse() local RunService = game:GetService("RunService") local MaxDegree = math.rad(80)

    local currentCam = 0 local connection

    local function updateAccessoryTransparency(target, isTransparent) for _, child in target:GetChildren() do if child:IsA("Accessory") then updateAccessoryTransparency(child, isTransparent) elseif child:IsA("BasePart") then local hasRelevantAttachment = child:FindFirstChild("HatAttachment") or child:FindFirstChild("FaceCenterAttachment") or child:FindFirstChild("FaceFrontAttachment") or child:FindFirstChild("FaceRearAttachment") or child:FindFirstChild("HairAttachment") or child:FindFirstChild("NeckAttachment") or child:FindFirstChild("WaistFrontAttachment")

    		if hasRelevantAttachment then
    			child.LocalTransparencyModifier = isTransparent and 1 or child.Transparency
    		end
    	end
    end
    

    end

    local function updateCharacterTransparency(character, isTransparent) for _, part in character:GetChildren() do if part:IsA("BasePart") then if isTransparent or part.Name == "Head" then part.LocalTransparencyModifier = 1 else part.LocalTransparencyModifier = part.Transparency end updateAccessoryTransparency(part, isTransparent) elseif part:IsA("Folder") or part:IsA("Model") then updateAccessoryTransparency(part, isTransparent) end end end

    local Exceptions = { FP = function() if currentCam == 0 then currentCam += 1 Camera.FieldOfView = 70 Camera.CameraSubject = Character:FindFirstChild("Head") updateAccessoryTransparency(Character) elseif currentCam == 1 then currentCam += 1 updateAccessoryTransparency(Character, true) updateCharacterTransparency(Character, true) Camera.FieldOfView = 90 connection = RunService.RenderStepped:Connect(function() local mousePos = Vector2.new(Mouse.X, Mouse.Y) local screenCenter = Camera.ViewportSize / 2 local delta = mousePos - screenCenter

    			local angle = Vector2.new((delta.Y / screenCenter.Y) * MaxDegree, (delta.X / screenCenter.X) * MaxDegree)
    			Camera.CFrame = Character.Head.CFrame * CFrame.new(0, 0, (-Character.Head.Size.Z / 2) * 1.25) * CFrame.Angles(-angle.X, -angle.Y, 0)
    		end)
    	elseif currentCam == 2 then
    		connection:Disconnect()
    		updateAccessoryTransparency(Character)
    		currentCam = 0
    		Camera.FieldOfView = 70
    		Camera.CameraSubject = Humanoid
    	end
    end,
    ExcludedFrame2 = function() 
    	-- templt
    end,
    

    }

    local function onButtonClick(button) if Exceptions[button.Name] then Exceptionsbutton.Name else local targetFrame = MainFrame.Parent:FindFirstChild(button.Name) if targetFrame and targetFrame:IsA("Frame") then targetFrame.Visible = true print("Frame named '" .. targetFrame.Name .. "' is now visible.") else warn("Frame named '" .. button.Name .. "' not found.") end end end

    for _, child in pairs(MainFrame:GetChildren()) do if child:IsA("TextButton") then child.MouseButton1Click:Connect(function() onButtonClick(child) end) end end

    is there any easier way to handle the FP function without the need of any external functions?

  2. Assistant

    Yes, you can simplify the FP function by consolidating the code and reducing redundancy. This will eliminate the need for the external accessory and character transparency update functions, as these can be directly incorporated within the FP function logic. Below is a more streamlined version:

    local Player = game.Players.LocalPlayer
    local Character = Player.Character
    local MainFrame = script.Parent
    local Camera = workspace.CurrentCamera
    local Humanoid = Character:WaitForChild("Humanoid")
    local Mouse = Player:GetMouse()
    local RunService = game:GetService("RunService")
    local MaxDegree = math.rad(80)
    
    local currentCam = 0
    local connection
    
    local function updateTransparency(parts, transparency)
    	for _, part in pairs(parts) do
    		if part:IsA("BasePart") then
    			part.LocalTransparencyModifier = transparency
    		elseif part:IsA("Accessory") or part:IsA("Folder") or part:IsA("Model") then
    			updateTransparency(part:GetChildren(), transparency)
    		end
    	end
    end
    
    local function FP()
    	if currentCam == 0 then
    		currentCam += 1
    		Camera.FieldOfView = 70
    		Camera.CameraSubject = Character:FindFirstChild("Head")
    		updateTransparency(Character:GetChildren(), 0)
    	elseif currentCam == 1 then
    		currentCam += 1
    		updateTransparency(Character:GetChildren(), 1)
    
    		Camera.FieldOfView = 90
    		connection = RunService.RenderStepped:Connect(function()
    			local mousePos = Vector2.new(Mouse.X, Mouse.Y)
    			local screenCenter = Camera.ViewportSize / 2
    			local delta = mousePos - screenCenter
    
    			local angle = Vector2.new((delta.Y / screenCenter.Y) * MaxDegree, (delta.X / screenCenter.X) * MaxDegree)
    			Camera.CFrame = Character.Head.CFrame * CFrame.new(0, 0, (-Character.Head.Size.Z / 2) * 1.25) * CFrame.Angles(-angle.X, -angle.Y, 0)
    		end)
    	elseif currentCam == 2 then
    		if connection then connection:Disconnect() end
    		updateTransparency(Character:GetChildren(), 0)
    		currentCam = 0
    		Camera.FieldOfView = 70
    		Camera.CameraSubject = Humanoid
    	end
    end
    
    local Exceptions = {
    	FP = FP,
    	ExcludedFrame2 = function() 
    		-- template
    	end,
    }
    
    local function onButtonClick(button)
    	if Exceptions[button.Name] then
    		Exceptions[button.Name]()
    	else
    		local targetFrame = MainFrame.Parent:FindFirstChild(button.Name)
    		if targetFrame and targetFrame:IsA("Frame") then
    			targetFrame.Visible = true
    			print("Frame named '" .. targetFrame.Name .. "' is now visible.")
    		else
    			warn("Frame named '" .. button.Name .. "' not found.")
    		end
    	end
    end
    
    for _, child in pairs(MainFrame:GetChildren()) do
    	if child:IsA("TextButton") then
    		child.MouseButton1Click:Connect(function()
    			onButtonClick(child)
    		end)
    	end
    end
    

    Explanation:

    1. Update Transparency: A single function updateTransparency is used to set the transparency recursively on all relevant parts and accessories.
    2. FP Function: This function handles the camera modes and appropriately updates transparency locally.
    3. Camera Movement Update: This is handled within the same function as part of the RenderStepped connection.

    This reduces the complexity and makes the function self-contained.

  3. User

    it doesnt work exactly the same.

  4. Assistant

    I apologize for the inconvenience. Let's rework it while ensuring we maintain the same functionality and handle transparency correctly. We will keep the transparency logic concise within the FP function while ensuring parts and accessories are updated properly.

    Here's the detailed approach:

    1. Transparency Logic: We need to ensure we adjust both primary parts and their child accessories accurately.
    2. Camera Handling: Properly set up the first-person camera and handle transition back to the regular camera view.

    Below is the revised script with the necessary adjustments to maintain the same functionality:

    local Player = game.Players.LocalPlayer
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local MainFrame = script.Parent
    local Camera = workspace.CurrentCamera
    local Humanoid = Character:WaitForChild("Humanoid")
    local Mouse = Player:GetMouse()
    local RunService = game:GetService("RunService")
    local MaxDegree = math.rad(80)
    
    local currentCam = 0
    local connection
    
    local function setTransparency(target, transparency)
    	for _, child in ipairs(target:GetChildren()) do
    		if child:IsA("BasePart") then
    			child.LocalTransparencyModifier = transparency
    		elseif child:IsA("Accessory") or child:IsA("Folder") or child:IsA("Model") then
    			setTransparency(child, transparency)
    		end
    	end
    end
    
    local function FP()
    	if currentCam == 0 then
    		currentCam += 1
    		Camera.FieldOfView = 70
    		Camera.CameraSubject = Character:FindFirstChild("Head")
    		setTransparency(Character, 0)
    	elseif currentCam == 1 then
    		currentCam += 1
    		for _, part in pairs(Character:GetChildren()) do
    			if part:IsA("BasePart") then
    				if part.Name ~= "Head" then
    					part.LocalTransparencyModifier = 1
    				else
    					part.LocalTransparencyModifier = 0
    				end
    			end
    		end
    		Camera.FieldOfView = 90
    		connection = RunService.RenderStepped:Connect(function()
    			local mousePos = Vector2.new(Mouse.X, Mouse.Y)
    			local screenCenter = Camera.ViewportSize / 2
    			local delta = mousePos - screenCenter
    
    			local angle = Vector2.new((delta.Y / screenCenter.Y) * MaxDegree, (delta.X / screenCenter.X) * MaxDegree)
    			Camera.CFrame = Character.Head.CFrame * CFrame.new(0, 0, (-Character.Head.Size.Z / 2) * 1.25) * CFrame.Angles(-angle.X, -angle.Y, 0)
    		end)
    	elseif currentCam == 2 then
    		if connection then connection:Disconnect() end
    		setTransparency(Character, 0)
    		currentCam = 0
    		Camera.FieldOfView = 70
    		Camera.CameraSubject = Humanoid
    	end
    end
    
    local Exceptions = {
    	FP = FP,
    	ExcludedFrame2 = function() 
    		-- template
    	end,
    }
    
    local function onButtonClick(button)
    	if Exceptions[button.Name] then
    		Exceptions[button.Name]()
    	else
    		local targetFrame = MainFrame.Parent:FindFirstChild(button.Name)
    		if targetFrame and targetFrame:IsA("Frame") then
    			targetFrame.Visible = true
    			print("Frame named '" .. targetFrame.Name .. "' is now visible.")
    		else
    			warn("Frame named '" .. button.Name .. "' not found.")
    		end
    	end
    end
    
    for _, child in pairs(MainFrame:GetChildren()) do
    	if child:IsA("TextButton") then
    		child.MouseButton1Click:Connect(function()
    			onButtonClick(child)
    		end)
    	end
    end
    

    Key Points:

    1. setTransparency Function: Recursively sets the transparency of all relevant parts and accessories.
    2. FP Function:
      • First State: Sets the camera to the player's head and adjusts the field of view.
      • Second State: Sets the head transparency to 0 and the other parts to 1.
      • Third State: Disconnects the camera update, resets transparency, and sets the camera back to the humanoid.
    3. Event Connection: Handles the mouse click events to update the user interface or switch camera modes.

    This ensures a more streamlined and functioning script while retaining the essential logic.