Why are my mobile buttons disappearing after a player resets character?

  1. What do you want to achieve?
    I want to have a button which toggles ‘enable’ for a GUI, for mobile users

  2. What is the issue?
    The button works fine, however, whenever a player resets character, the buttons disappear.

  3. What solutions have you tried so far?
    I have attempted various ways of using BindAction with different functions, but as mentioned, it works fine for as long as a character is not reset.

The code I have is pretty much ‘vanilla’ code. I have the LocalScript in StarterPlayer->StarterPlayerScripts and am wondering whether that is the issue?! If this is the issue, where would I need to put it?

As per documentation, the StartPlayerScripts run only once ,in the beginning. Could this be the issue on why when a player resets their character, the buttons disappear?

Here is my code (hopefully it’s formatted fine):

local LocalPlayer = game.Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local contextAction = game:GetService("ContextActionService")

local ReplicatedFirst = game:GetService("ReplicatedFirst")

local function onBKeyPress(actionName, userInputState, inputObject)
local platform = ReplicatedFirst:WaitForChild("Platform", 10)
	
	if userInputState == Enum.UserInputState.Begin then		
		if platform.PC.Value == true then
			PlayerGui.ShopGui:FindFirstChild("PC_ShopGui").Enabled = not PlayerGui.ShopGui:FindFirstChild("PC_ShopGui").Enabled
		elseif platform.Mobile.Value == true then
			PlayerGui.ShopGui:FindFirstChild("Mobile_ShopGui").Enabled = not PlayerGui.ShopGui:FindFirstChild("Mobile_ShopGui").Enabled
		end	
	end
end

contextAction:BindAction("keyPressB", onBKeyPress, true, Enum.KeyCode.B)

Thank you for the help!

1 Like

Try setting the Screen Gui’s (ShopGui) ResetOnSpawn to false.

2 Likes

I had tried that as well, and unfortunately it did not work. After doing that, I confirmed that the gui was indeed correct ( had the loaded data from replicatedfirst as expected). Therefore, I concluded that this issue is unrelated to the state of the gui, but more on the buttons somehow.

(Have to sleep, it’s 2:30am local time and have to work in a few hours). Will be checking thread throughout the day, thank you again.

I have created a repro, in addition, I have a Place published that shows what I’m experiencing.

Please take a look at the following Place:

I set it to allow copies.

Here is repro.

Repro.rbxl (27.3 KB)

Steps To Reproduce:

1) Log in to Place from Mobile

Note the button on top of the JUMP button on the mid-bottom-right.

Click on it to see that it functions as expected (No data is shown by design)

2) Do a character reset

3) Now, note that the button is no longer there?!

I’m stuck here and can’t figure out what can be causing this.

I will continue troubleshooting and if I find the solution, I’ll post it here.

Thank you.

I’m curious to see if this is about the reset or the death/respawn.
If you take damage and die, does this occur? Do you have something connected to the reset callback that might have an impact here?

@boatbomber - I added a ‘max damage’ button which kills a player on server side and ran it on mobile; unfortunately, issue still persists.

2 Likes

I believe it has to do with your platform value being in replicated first, but I can’t confirm this as I’m on mobile right now. Maybe try putting it in lighting under their username.

It might have something to do with the CoreScripts.

This happened after I reset.

@Mrflashmaster21 - I tested with data outside replicatedfirst and got the same issue.

@0_1195 - Oh… this is a great finding! I’ll look into that later tonight when I get the chance!

I found a ‘solution’.

After hours and hours of trying, I just went ahead and created one SIMPLE mobile button, and tried resetting the character from my mobile, and lo and behold, it stayed! My other mobile button disappeared.

So what was the difference between these two localscripts? The difference was the location of the localscript containing the ContentActionService BindAction rather than the content of the localscripts themselves.

To test this out, I went ahead and moved my original localscript containing the code I added to this thread, to the StarterGui folder, and it worked!

So going through some analysis here:

  1. Having the script in StarterPlayerScripts was not reloading the mobile buttons/bindactions back into the game after a character reset or death
  2. Having the script in StarterGui is reloading the bindactions after a reset/death

My question here is whether for solution #1, we would need to manually reload these bindactions back in, because it appears like they are not doing so automatically.

I will set this as the solution as at least it allows me to continue on.

Edit: See @colbert2677 great explanation below for further details.

This probably happens because of the behaviour of both containers that I wouldn’t completely rely on. The contents of StarterGui are copied to the character’s PlayerGui when their character value changes (internally recognised as a respawn), while StarterPlayerScripts does not reset per spawn.

Conventionally, having code in StarterGui is weird if it isn’t Gui related. Even though you can bind a button from ContextActionService, that’s more of an add-on than its main function. Overall though it’s your decision what you like to do with your code so don’t take my word as authoritative.

Just so you’re aware, the PlayerModule (the one that sets up player controls and the camera) are bound via StarterPlayerScripts. Why you’re able to see mobile controls after death despite the script’s location is because it rebinds actions when a player respawns. It has a set of methods dedicated to actions to be taken upon any respawns or changes to the character.

So your question has an answer: yes you would. If you have your LocalScript in StarterPlayerScripts, you’re responsible for handling respawn conditions and reloading the button when a player respawns.

2 Likes

@colbert2677 - Thank you for the information, very helpful.

I’ll plan to refactor the logic out of these locations.

1 Like

This worked for me. (The scripts were inside of a Gui in PlayerGui) thx