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 --[[and child.Name ~= "Torso"]] then
    			child.LocalTransparencyModifier = isTransparent and 1 or child.Transparency
    		end
    	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) 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,
    ToggleUIButton = function()
    	MainFrame.Position = UDim2.new(-1, 0, 0.5, 0)
    end,
    

    }

    local function hideFrames(targetFrame) local framesToHide = {"Morph", "Animation", "Walkspeed", "House"} for _, frameName in ipairs(framesToHide) do if frameName ~= targetFrame then local frame = MainFrame.Parent:FindFirstChild(frameName) if frame and frame:IsA("Frame") then frame.Visible = false end end end 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 if not targetFrame.Visible then targetFrame.Visible = true if button.Name == "Morph" or button.Name == "Animation" or button.Name == "Walkspeed" or button.Name == "House" then hideFrames(button.Name) end else targetFrame.Visible = false end else warn("Frame named '" .. button.Name .. "' not found.") end end end

    for _, child in MainFrame:GetChildren() do if child:IsA("TextButton") then child.MouseEnter:Connect(function() if child.ButtonName then child.ButtonName.Visible = true end end) child.MouseLeave:Connect(function() if child.ButtonName then child.ButtonName.Visible = false end end) onButtonClick(child) end end

    can you tell what is wrong with this code?

  2. Assistant

    The script you provided appears to be a GUI-related script for controlling a camera view and GUI elements in a Roblox game. After reviewing it, here are a few issues and some suggestions for improvement:

    1. Initialization of currentCam:

      • You initialize currentCam to 0 outside the Exceptions.FP function, which may cause unintended behavior if the script runs multiple times.
    2. Disconnecting the connection correctly:

      • Ensure that the connection variable is properly handled. If connection is not nil, you should disconnect it before reassigning it or resetting it.
    3. Updating transparency method issues:

      • Ensure that updateAccessoryTransparency(Character) is correctly being called with the right boolean value to reset transparency.
    4. Executing button clicks on initialization:

      • The onButtonClick(child) call within the loop iterating over MainFrame children actually invokes the button click function for all buttons upon script initialization, which might not be what you intend.
    5. Proper handling of UI Positions:

      • Ensure the MainFrame.Position is being updated as intended within the ToggleUIButton function.
    6. Handling nil connections:

      • Always check if connection is non-nil before trying to disconnect it.
    7. Using RunService.RenderStepped correctly:

      • Verify that the use of RunService.RenderStepped is appropriate for the operations being done each frame.

    Here is a refined version of your script with the considerations mentioned above:

    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 setCameraToHead()
    	Camera.FieldOfView = 70
    	Camera.CameraSubject = Character:FindFirstChild("Head")
    end
    
    local function resetCamera()
    	Camera.FieldOfView = 70
    	Camera.CameraSubject = Humanoid
    	updateAccessoryTransparency(Character, false)
    	currentCam = 0
    end
    
    local function FP()
    	if currentCam == 0 then
    		currentCam += 1
    		setCameraToHead()
    		updateAccessoryTransparency(Character)
    	elseif currentCam == 1 then
    		currentCam += 1
    		updateAccessoryTransparency(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
    		if connection then
    			connection:Disconnect()
    			connection = nil
    		end
    		resetCamera()
    	end
    end
    
    local function ToggleUIButton()
    	MainFrame.Position = UDim2.new(-1, 0, 0.5, 0)
    end
    
    local function hideFrames(targetFrame)
    	local framesToHide = {"Morph", "Animation", "Walkspeed", "House"}
    	for _, frameName in ipairs(framesToHide) do
    		if frameName ~= targetFrame then
    			local frame = MainFrame.Parent:FindFirstChild(frameName)
    			if frame and frame:IsA("Frame") then
    				frame.Visible = false
    			end
    		end
    	end
    end
    
    local Exceptions = {
    	FP = FP,
    	ToggleUIButton = ToggleUIButton,
    }
    
    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
    			if not targetFrame.Visible then
    				targetFrame.Visible = true
    				if button.Name == "Morph" or button.Name == "Animation" or button.Name == "Walkspeed" or button.Name == "House" then
    					hideFrames(button.Name)
    				end
    			else
    				targetFrame.Visible = false
    			end
    		else
    			warn("Frame named '" .. button.Name .. "' not found.")
    		end
    	end
    end
    
    for _, child in MainFrame:GetChildren() do
    	if child:IsA("TextButton") then
    		child.MouseEnter:Connect(function()
    			if child.ButtonName then
    				child.ButtonName.Visible = true
    			end
    		end)
    		child.MouseLeave:Connect(function()
    			if child.ButtonName then
    				child.ButtonName.Visible = false
    			end
    		end)
    		child.MouseButton1Click:Connect(function()
    			onButtonClick(child)
    		end)
    	end
    end
    

    Explanation of Improvements:

    1. Connection Handling: Safely disconnecting connection by checking if it's not nil before trying to disconnect it.
    2. Initialization: Removed onButtonClick(child) during loop execution; now it only connects the click event.
    3. Update Method: Ensured updateAccessoryTransparency is called with the right boolean value to reset transparency.
    4. Camera Handling: Simplified setting and resetting the camera field of view and subject by using helper functions (setCameraToHead, resetCamera).

    Hopefully, these changes address the issues in your script and improve its functionality.