How do I get this script to work?

I’m having issues getting this LocalScript to work in a TextButton inside of a SurfaceGui. I’ve tried searching around for help, and I know it has to do with the PlayerGui, but I’m not sure how to get it to work.

When this script is placed inside of a TextButton inside of a ScreenGui, it works completely fine. When it’s placed into a SurfaceGui, though, it doesn’t work. How can I fix this?

local Workspace = game:GetService("Workspace")
local CameraPart = Workspace:WaitForChild("CameraPart")
local TweenService = game:GetService("TweenService")
local Btn = script.Parent
local Camera = Workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer

local tweenInformation = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

Btn.Activated:Connect(function()
	if Camera.CFrame ~= CameraPart.CFrame then
		Camera.CameraType = Enum.CameraType.Scriptable
		local Tween = TweenService:Create(Camera, tweenInformation, {CFrame = CameraPart.CFrame})
		Tween:Play()

	elseif Camera.CFrame == CameraPart.CFrame then
		local Tween = TweenService:Create(Camera, tweenInformation, {CFrame = Player.Character.Head.CFrame})
		Tween:Play()
		Tween.Completed:Connect(function()
			Camera.CameraType = Enum.CameraType.Custom
		end)
	end
end)

image

Localscripts only run when its a descendant of an object that directly relate to the player.

1 Like

From LocalScripts article in developer.roblox.com, Localscripts only work on

  • A Player’s Backpack, such as a child of a Tool
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts.
  • The ReplicatedFirst service.

To get this to work parent the SurfaceGUI to StarterGui and set the Adornee.

LocalScripts don’t run in the workspace. Instead, you need to put the LocalScript either in StarterGui or StarterPlayerScripts. The localscript can then define the textbutton, and handle the rest.

A good way to do this is to put a script inside the meshpart, then put the localscript inside of it. Put the following code inside the script:

game.Players.PlayerAdded:Connect(function(p)
    local button = script.Parent.SurfaceGui.TextButton
    local clone = script.LocalScript:Clone()
    local value = Instance.new("ObjectValue")
    value.Parent=clone
    value.Value = button
    value.Name="Button"
    clone.Parent = p.PlayerGui
end)

And in the localscript, make these revisions:

local Workspace = game:GetService("Workspace")
local CameraPart = Workspace:WaitForChild("CameraPart")
local TweenService = game:GetService("TweenService")
local Btn = script:WaitForChild("Button").Value
local Camera = Workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer

local tweenInformation = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

Btn.Activated:Connect(function()
	if Camera.CFrame ~= CameraPart.CFrame then
		Camera.CameraType = Enum.CameraType.Scriptable
		local Tween = TweenService:Create(Camera, tweenInformation, {CFrame = CameraPart.CFrame})
		Tween:Play()

	elseif Camera.CFrame == CameraPart.CFrame then
		local Tween = TweenService:Create(Camera, tweenInformation, {CFrame = Player.Character.Head.CFrame})
		Tween:Play()
		Tween.Completed:Connect(function()
			Camera.CameraType = Enum.CameraType.Custom
		end)
	end
end)

Let me know how this works!

Put the localscript inside of what? The script or the meshpart?

The script, sorry for not clarifying.

Not working. Here’s what I have.

Script:

local plr = game:GetService("Players").LocalPlayer

    game.Players.PlayerAdded:Connect(function(p)
    	local button = script.Parent.SurfaceGui.TextButton
    	local clone = script.LocalScript:Clone()
    	local value = Instance.new("ObjectValue")
    	value.Parent=clone
    	value.Value = button
    	clone.Parent = plr.PlayerGui
    end)

LocalScript:

local Workspace = game:GetService("Workspace")
local CameraPart = Workspace:WaitForChild("CameraPart")
local TweenService = game:GetService("TweenService")
local Btn = script.ObjectValue.Value
local Camera = Workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer

local tweenInformation = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

Btn.Activated:Connect(function()
	if Camera.CFrame ~= CameraPart.CFrame then
		Camera.CameraType = Enum.CameraType.Scriptable
		local Tween = TweenService:Create(Camera, tweenInformation, {CFrame = CameraPart.CFrame})
		Tween:Play()

	elseif Camera.CFrame == CameraPart.CFrame then
		local Tween = TweenService:Create(Camera, tweenInformation, {CFrame = Player.Character.Head.CFrame})
		Tween:Play()
		Tween.Completed:Connect(function()
			Camera.CameraType = Enum.CameraType.Custom
		end)
	end
end)

image

Remove this from the script. Should solve the issue.

Still nope. Here’s the script now.
image

I see the issue I made. I changed the server script, and it should work now. Check the edit I made and replace your script.

Doesn’t work, but there’s output for the LocalScript.

Really sorry about all the issues. I edit both the server and local script, and THIS time it should work :grin:

It still doesn’t work. It’s alright if we can’t get it to work, I appreciate the effort!

It’s okay, let’s keep trying. Any errors in the output?

Nope, nothing.

Hmmm. I tried it in another game and it seems to work. It could be another issue with your code itself and not the button activation. Try adding a print('hi') after the Btn.Activated event to see if it is triggering it. Also make sure that CameraPart is in the workspace and it is not falling into an infinite yield.

But also make sure you have the same script in the server and client as I do in my above post.

1 Like

Ohh! The camera part wasn’t anchored. Thank you so much for the help, you’re a lifesaver!

1 Like

There we go :slight_smile: I’m glad we were able to solve the issue together. Good luck!

1 Like