- What do you want to achieve? Keep it simple and clear!
I am trying to make a GUI button to start/stop my game.
- 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.
- 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?