My scripts randomly stop working after I reset my character

Few days ago I scripted the UI for my game. Today I noticed really weird thing; when I reset or I die, the UI scripts stop working. After resetting or dying, the buttons on the right side stop working as you can see from this video:

Those UIs are handled by 2 scripts:

LocalScript 'CodesUI' (Twitter Codes UI)
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer

local mainUI = LocalPlayer.PlayerGui:WaitForChild("UI"):WaitForChild("MainUI")
local codesButton = mainUI:WaitForChild("MidRight"):WaitForChild("CodesFrame"):WaitForChild("CodesButton")
local codesFrame = LocalPlayer.PlayerGui:WaitForChild("UI"):WaitForChild("CodeUI"):WaitForChild("Frame")
local closeButton = codesFrame:WaitForChild("Top"):WaitForChild("ExitFrame"):WaitForChild("ExitButton")

local closeCodesUIBEvent = script.Parent.Parent.UIEvents.CodesUI.CloseCodesUI
local closeShopUIBEvent = script.Parent.Parent.UIEvents.ShopUI.CloseShopUI

local isCodesUIOpen = false

local function openCodesUI()
	closeShopUIBEvent:Fire()
	isCodesUIOpen = true
	codesFrame:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 1.5, true)
end

local function closeCodesUI()
	isCodesUIOpen = false
	codesFrame:TweenPosition(UDim2.new(1.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 1.5, true)
end

codesButton.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		if input.UserInputState == Enum.UserInputState.Begin then
			if not isCodesUIOpen then
				openCodesUI()
			else
				closeCodesUI()
			end
		end
	end
end)

closeButton.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		if input.UserInputState == Enum.UserInputState.Begin then
			closeCodesUI()
		end
	end
end)

closeCodesUIBEvent.Event:Connect(function()
	closeCodesUI()
end)
LocalScript 'ShopUI' (Shop UI)
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer

local mainUI = LocalPlayer.PlayerGui:WaitForChild("UI"):WaitForChild("MainUI")
local shopUI = LocalPlayer.PlayerGui:WaitForChild("UI"):WaitForChild("ShopUI")
local shopFrame = shopUI:WaitForChild("Frame")
local coinShop = shopFrame:WaitForChild("CoinScreen")
local gamepassShop = shopFrame:WaitForChild("PassesScreen")
local shopButton = mainUI:WaitForChild("MidRight"):WaitForChild("ShopFrame"):WaitForChild("ShopButton")
local closeShopButton = shopFrame:WaitForChild("Top"):WaitForChild("Frame"):WaitForChild("ExitFrame"):WaitForChild("ExitButton")
local coinShopButton = shopFrame:WaitForChild("Top"):WaitForChild("Frame"):WaitForChild("2Coins")
local gamepassShopButton = shopFrame:WaitForChild("Top"):WaitForChild("Frame"):WaitForChild("1Gamepasses")

local closeCodesUIBEvent = script.Parent.Parent.UIEvents.CodesUI.CloseCodesUI
local closeShopUIBEvent = script.Parent.Parent.UIEvents.ShopUI.CloseShopUI

local isShopUIOpen = false
local isCoinShopOpen = false

local function openShopUI()
	closeCodesUIBEvent:Fire()
	isShopUIOpen = true
	shopFrame:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 1.5, true)
end

local function closeShopUI()
	isShopUIOpen = false
	shopFrame:TweenPosition(UDim2.new(1.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 1.5, true)
end

local function openCoinShop()
	isCoinShopOpen = true
	gamepassShop.Visible = false
	coinShop.Visible = true
end

local function openGamepassShop()
	isCoinShopOpen = false
	coinShop.Visible = false
	gamepassShop.Visible = true
end

shopButton.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		if input.UserInputState == Enum.UserInputState.Begin then
			if not isShopUIOpen then
				openShopUI()
			else
				closeShopUI()
			end
		end
	end
end)

closeShopButton.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		if input.UserInputState == Enum.UserInputState.Begin then
			closeShopUI()
		end
	end
end)

gamepassShopButton.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		if input.UserInputState == Enum.UserInputState.Begin then
			if isCoinShopOpen then
				openGamepassShop()
			end
		end
	end
end)

coinShopButton.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		if input.UserInputState == Enum.UserInputState.Begin then
			if not isCoinShopOpen then
				openCoinShop()
			end
		end
	end
end)

closeShopUIBEvent.Event:Connect(function()
	closeShopUI()
end)

I have read something about “ResetOnSpawn” property of ScreenGui. However it doesn’t help is it ticked or not.

Check the resetonspawn property under the screengui

It’s not checked. And as I said before I tried it with both without resetonspawn and with resetonspawn and no difference.

Where are both of those LocalScripts located?

game.StarterPlayer.StarterPlayerScripts.Common.UIs

I assuming there is another script that is handling the click sounds and button ‘click’ tweens since I can’t find any of that in the scripts above. Could it possibly be interfering?

There is another script that handles button animations and sounds through CollectionService.
I tried removing it but it didn’t help.

This is the animation and sound script:

local CollectionService = game:GetService("CollectionService")
local SoundService = game:GetService("SoundService")

local soundEffects = SoundService.SoundEffects
local uiButtonClick = soundEffects.Sounds:FindFirstChild("UIButtonClick")

-- UIButton
local function tagUIButton(button)
	button.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
			if input.UserInputState == Enum.UserInputState.Begin then
				button:TweenPosition(UDim2.new(0, 0, 0.1, 0), "In", "Linear", 0.1, true)
				uiButtonClick:Play()
			end
		end
	end)

	button.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
			button:TweenPosition(UDim2.new(0, 0, 0, 0), "In", "Linear", 0.1, true)
		end
	end)
end

for _, button in pairs(CollectionService:GetTagged("UIButton")) do
	tagUIButton(button)
end

CollectionService:GetInstanceAddedSignal("UIButton"):Connect(tagUIButton)

--UIButton2
local function tagUIButton2(button)
	button.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
			if input.UserInputState == Enum.UserInputState.Begin then
				button:TweenPosition(UDim2.new(0, 0, 0.05, 0), "In", "Linear", 0.1, true)
				uiButtonClick:Play()
			end
		end
	end)

	button.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
			button:TweenPosition(UDim2.new(0, 0, 0, 0), "In", "Linear", 0.1, true)
		end
	end)
end

for _, button in pairs(CollectionService:GetTagged("UIButton2")) do
	tagUIButton2(button)
end

CollectionService:GetInstanceAddedSignal("UIButton2"):Connect(tagUIButton2)

Have you tried setting IsCodeUIOpen to false after?

I made an empty baseplate and used your script to open and close a dummy UI. It worked properly, could there be anything else in your place that might be causing this? Gui Test.rbxl (23.1 KB)


You could try moving your UIs over to a baseplate with only the required stuff and try it there.

I moved my UIs to the baseplate and that starts to happen there as well.

Ok, could you try setting some breakpoints in your code and then step through it with the debugger? This’ll allow you to see the values of variables as the code runs.

I think it’d be easiest to start with your CodesGui as it seems the simplest. Try setting them inside the InputBegan events (specifically the button that opens it) and see what going on.

image
I set the following breakpoints and they did activate properly when I hasn’t died yet. After I reset my character they didn’t activate.
I also set a breakpoint on line 27 and it didn’t work after resetting as well.

It seems like .InputBegan doesn’t fire after I reset my character.

Hmm, I’m not sure why that is happening. Something you could try though is switch from the InputBegan event to the Activated event instead. I use the activated event for all my buttons in my game and havn’t had any problems.

I changed it to Activated, doesn’t help at all.

Your should hold all code for a UI inside a local script of the UI itself, this would prevent replication errors after death.

Chances are its probably an error in regards to your local scripts still pointing to an old instance of the UI. That being said, it is then operating on something which has just been refreshed e.g. isn’t the same thing anymore.

In that case, I’m out of ideas for the time being.

I think there is something replacing the UI which is causing the events to not fire as they’re set to the original one, but I have no idea what it could be at this point.

I need to use a package since I have 2 places in my game and having one script inside one button would be pain with packages. I don’t want to copy code manually between places.

What do you mean packages?? Local scripts focused on a single UI should only really effect that UI. UIs are there to act as interfaces…

Packages are Packages | Documentation - Roblox Creator Hub

Do you have any errors at all?