GUI button is broken after respawn

  1. What do you want to achieve? Keep it simple and clear!

I am trying to make a GUI button to start/stop my game.

  1. What is the issue? Include screenshots / videos if possible!

The local script gets reset when the player is respawned, but I want it to keep the variable gameRunning in order to send the correct remote event.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried putting the script in ReplicatedFirst, inside the GUI of the button, and in StartPlayerScripts

local startGameEvent = game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("startGame")
local stopGameEvent = game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("stopGame")
local player = game:GetService("Players").LocalPlayer
local gui = player:WaitForChild("PlayerGui"):WaitForChild("GUI")
local button = gui:WaitForChild("Button")
local gameRunning = false


local function startGame()
	print("pressed")
	if gameRunning == true then
		button.Text = "Stop"
		gameRunning = false
		stopGameEvent:FireServer()
	elseif gameRunning == false then
		button.Text = "Start"
		startGameEvent:FireServer()
		gameRunning = true
	end
	
end

local function loadGui()
	button.Activated:Connect(startGame)
	if gameRunning == true then
		button.Text = "Stop"
	elseif gameRunning == false then
		button.Text = "Start"
	end
end

player.Character:WaitForChild("Humanoid").Died:Connect(function()
	loadGui()
end)



loadGui()

How can I keep this script after destroying the character or save the variable clientside to be loaded each time?

1 Like
  1. If the script is inside StarterPlayerScripts it isn’t supposed to reset.
  2. If your script is inside a UI you should set UI.ResetOnSpawn to false to prevent scripts inside from resetting.
1 Like

I’m not sure what the issue ended up being, but it is working now. I had the settings UI.ResetOnSpawn set to false already. Thanks for the help.

1 Like

Yeah, that should be set to true & move the script to the StarterCharacterScripts folder if it should execute each time the player’s character is reloaded/spawned.

1 Like