Duplicated Scripts don't work

I have 2 LocalScripts that I use to Open and Close a ScreenGui called ShopUi, this works perfectly with no issues but after making another Menu Popup called SettingsUi and duplicating everything correctly it just doesn’t work no errors or anything, and even with my limited understanding of Lua I know this shouldn’t be happening.

Even weirder, the first time I duplicated the scripts the one in SettingsUi worked fine when the one in the Settings button didn’t. So this is all wizardry for me as why something that worked one time stops working the second time, I maybe missed something but right now I’m lost.

The original scripts for the ShopUi:

In the Shop button:

local button = script.Parent
local ShopUi = game.Players.LocalPlayer.PlayerGui:FindFirstChild("ShopUi")
local Main = ShopUi and ShopUi:FindFirstChild("Main")
local TweenService = game:GetService("TweenService")
local originalPosition = Main and Main.Position or UDim2.new(0, 0, 0, 0)
local animationPlaying = false

local function createBounceAnimation(target, info)
	local tweenInfo = TweenInfo.new(
		info.Time,
		Enum.EasingStyle.Back,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local tween = TweenService:Create(target, tweenInfo, info.Properties)
	return tween
end

if Main then
	Main.Position = UDim2.new(
		originalPosition.X.Scale,
		originalPosition.X.Offset,
		originalPosition.Y.Scale,
		originalPosition.Y.Offset + 500
	)
end

local function onButtonClick()
	if not animationPlaying and Main then
		animationPlaying = true
		local newYOffset = Main.Position.Y.Offset > originalPosition.Y.Offset and -500 or 500
		local animationInfo = {
			Time = 0.5,
			Properties = {
				Position = UDim2.new(
					originalPosition.X.Scale,
					originalPosition.X.Offset,
					originalPosition.Y.Scale,
					Main.Position.Y.Offset + newYOffset
				)
			}
		}
		local animation = createBounceAnimation(Main, animationInfo)
		animation:Play()
		animation.Completed:Connect(function()
			animationPlaying = false
		end)
	end
end

button.MouseButton1Click:Connect(onButtonClick)

And in the ShopUi:

local button = script.Parent
local ShopUi = game.Players.LocalPlayer.PlayerGui:FindFirstChild("ShopUi")
local Main = ShopUi and ShopUi:FindFirstChild("Main")
local TweenService = game:GetService("TweenService")
local originalPosition = Main and Main.Position or UDim2.new(0, 0, 0, 0)
local animationPlaying = false

local function createBounceAnimation(target, info)
	local tweenInfo = TweenInfo.new(
		info.Time,
		Enum.EasingStyle.Back,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local tween = TweenService:Create(target, tweenInfo, info.Properties)
	return tween
end

local function onButtonClick()
	if not animationPlaying and Main then
		animationPlaying = true
		local newYOffset = 500
		local animationInfo = {
			Time = 0.5,
			Properties = {
				Position = UDim2.new(
					originalPosition.X.Scale,
					originalPosition.X.Offset,
					originalPosition.Y.Scale,
					Main.Position.Y.Offset + newYOffset
				)
			}
		}
		local animation = createBounceAnimation(Main, animationInfo)
		animation:Play()
		animation.Completed:Connect(function()
			animationPlaying = false
			Main.Position = UDim2.new(originalPosition.X.Scale, originalPosition.X.Offset, originalPosition.Y.Scale, originalPosition.Y.Offset + newYOffset)
		end)
	end
end

button.MouseButton1Click:Connect(onButtonClick)

And now are the duplicated script with ShopUi changed to SettingsUi:

In the settings button:

local button = script.Parent
local SettingsUi = game.Players.LocalPlayer.PlayerGui:FindFirstChild("SettingsUi")
local Main = SettingsUi and SettingsUi:FindFirstChild("Main")
local TweenService = game:GetService("TweenService")
local originalPosition = Main and Main.Position or UDim2.new(0, 0, 0, 0)
local animationPlaying = false

local function createBounceAnimation(target, info)
	local tweenInfo = TweenInfo.new(
		info.Time,
		Enum.EasingStyle.Back,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local tween = TweenService:Create(target, tweenInfo, info.Properties)
	return tween
end

if Main then
	Main.Position = UDim2.new(
		originalPosition.X.Scale,
		originalPosition.X.Offset,
		originalPosition.Y.Scale,
		originalPosition.Y.Offset + 500
	)
end

local function onButtonClick()
	if not animationPlaying and Main then
		animationPlaying = true
		local newYOffset = Main.Position.Y.Offset > originalPosition.Y.Offset and -500 or 500
		local animationInfo = {
			Time = 0.5,
			Properties = {
				Position = UDim2.new(
					originalPosition.X.Scale,
					originalPosition.X.Offset,
					originalPosition.Y.Scale,
					Main.Position.Y.Offset + newYOffset
				)
			}
		}
		local animation = createBounceAnimation(Main, animationInfo)
		animation:Play()
		animation.Completed:Connect(function()
			animationPlaying = false
		end)
	end
end

button.MouseButton1Click:Connect(onButtonClick)

And in the SettingsUi:

local button = script.Parent
local SettingsUi = game.Players.LocalPlayer.PlayerGui:FindFirstChild("SettingsUi")
local Main = SettingsUi and SettingsUi:FindFirstChild("Main")
local TweenService = game:GetService("TweenService")
local originalPosition = Main and Main.Position or UDim2.new(0, 0, 0, 0)
local animationPlaying = false

local function createBounceAnimation(target, info)
	local tweenInfo = TweenInfo.new(
		info.Time,
		Enum.EasingStyle.Back,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local tween = TweenService:Create(target, tweenInfo, info.Properties)
	return tween
end

local function onButtonClick()
	if not animationPlaying and Main then
		animationPlaying = true
		local newYOffset = 500
		local animationInfo = {
			Time = 0.5,
			Properties = {
				Position = UDim2.new(
					originalPosition.X.Scale,
					originalPosition.X.Offset,
					originalPosition.Y.Scale,
					Main.Position.Y.Offset + newYOffset
				)
			}
		}
		local animation = createBounceAnimation(Main, animationInfo)
		animation:Play()
		animation.Completed:Connect(function()
			animationPlaying = false
			Main.Position = UDim2.new(originalPosition.X.Scale, originalPosition.X.Offset, originalPosition.Y.Scale, originalPosition.Y.Offset + newYOffset)
		end)
	end
end

button.MouseButton1Click:Connect(onButtonClick)

And here is the Hierarchy of my Ui:
Capture d'écran 2024-06-24 145959

Try changing the variable ShopUi for:

local ShopUi = game.Players.LocalPlayer.PlayerGui.SettingsUi

Even in the original scripts by doing that it still doesn’t work so it maybe has to do with the SettingsUi screenGui but it has the exact same properties as ShopUi

1 Like

You can try debugging using Print() and printing things as the code goes on

Well it seems like the scripts for SettingsUi are not registering when the buttons are clicked because when testing Print(“Test1”) in onButtonClick it doesn’t do anything when it works fine in the scripts for ShopUi

And again the buttons have the same properties

Update, it is registering the button click but there is an issue with the argument “if not animationPlaying and Main then”

Sorry just an error on my part, when trying to find a solution I renamed Main to Main1 which obviously broke everything now it works as intended

I kept checking the scripts in search of an error but it was just my Ui that was at fault

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.