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)
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)
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)
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.