Why my Script auto Disabled on died?

  1. What do you want to achieve?
    My script auto disabled when the character died, so i want to keep it enable all the time after i clicked the play button.

  2. What is the issue?
    Video of issue:
    Video - Google Drive

  3. What solutions have you tried so far?
    I am new to scripting through, i tried to add a localscript to make when the player health <= 0, the script disabled = false, but it not work.

Ima try my best to help you.

Where’s the code?

1 Like

Without the code, I can only assume so much.

Your script is a localscript in ScreenGUI, meaning it is refreshed upon a player’s death. There is a property in ScreenGUIs called ResetOnSpawn. This is, by default, turned on, meaning the GUI resets everytime the player resets. Localscripts (and scripts alike) do not share this property, and instead are recreated as they originally were in StarterGui.

Observe this video.

My code in plotselects:

local TweenService = game:GetService("TweenService")
local PlotService = require(game.ReplicatedStorage.PlotService)

local Player = game.Players.LocalPlayer
local Camera = game.Workspace.Camera

local PlotSelect = script.Parent:WaitForChild("PlotSelect")
local PlotAction = PlotSelect:WaitForChild("PlotAction")

local Frame = PlotSelect:WaitForChild("Frame")
local Left = Frame:WaitForChild("L")
local Right = Frame:WaitForChild("R")
local Select = Frame:WaitForChild("Select")

local SelectedPlot = Frame:WaitForChild("SelectedPlot")

local Plots = game.Workspace.Simworld.plot

local TI = TweenInfo.new(
	1,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

local function camTween(plot)
	local cf = CFrame.new(plot.Position+Vector3.new(0,120,0),plot.Position)
	local tween = TweenService:Create(game.Workspace.Camera,TI,{CFrame = cf})
	tween:Play()
end

local connections = {}

local function startPlotSelect()
	Frame.Visible = true
	Frame.Select.Text = "Select"
	
	Camera.CameraType = Enum.CameraType.Scriptable
    local plotsTable = Plots:GetChildren()

    local index = 1

    SelectedPlot.Value = plotsTable[1]
    camTween(SelectedPlot.Value)

connections[#connections+1] = Right.MouseButton1Click:Connect(function()
	if Plots:FindFirstChild("Plot" .. index-1) then
		index -= 1
	else
		
		index = #Plots:GetChildren()
	end
	
	SelectedPlot.Value = plotsTable[index]
	camTween(plotsTable[index])
end)

connections[#connections+1] = Left.MouseButton1Click:Connect(function()
	if Plots:FindFirstChild("Plot" .. index+1) then
		index += 1
	else
		index = 1
	end
	
	SelectedPlot.Value = plotsTable[index]
	camTween(plotsTable[index])
end)

connections[#connections+1] = Select.MouseButton1Click:Connect(function()
	local result = game.ReplicatedStorage.RequestPlot:InvokeServer(plotsTable[index])
	
	if result then
		Select.Text = "Success"
	else
		Select.Text = "Error"
	end
	
	Camera.CameraType = Enum.CameraType.Custom
	PlotSelect.Enabled = false
	SelectedPlot.Value = nil
		
	for i,v in pairs(connections) do
			if v then
				v:Disconnect()
			end
		end
		
		PlotSelect.Enabled = true
		Frame.Visible = false
		PlotAction.Text = "Sell Plot"
		PlotAction.Visible = true
    end)
end

startPlotSelect()

PlotAction.MouseButton1Click:Connect(function()
	local plot = PlotService:GetPlot(Player)
	
	if plot then
		game.ReplicatedStorage.SellPlot:FireServer()
		PlotAction.Text = "Buy Plot"
		PlotAction.Visible = true
	else
		startPlotSelect()
		PlotAction.Visible = false
	end
end)

My code in mainmenu gui:

local plr = game.Players.LocalPlayer

local char = plr.Character or plr.CharacterAdded:Wait()
char:WaitForChild("HumanoidRootPart").Anchored = true


local mouse = plr:GetMouse()


local camera = workspace.CurrentCamera

camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = workspace.MenuComponents.CameraStartPosition.CFrame


local tweenService = game:GetService("TweenService")


local mainMenuGui = script.Parent


local mainMenuBG = mainMenuGui:WaitForChild("MainMenuBackground")

local playBtn = mainMenuBG:WaitForChild("PlayButton")
local creditsBtn = mainMenuBG:WaitForChild("CreditsButton")

local creditsBG = mainMenuBG:WaitForChild("CreditsBackground")


local loadingScreenBG = mainMenuGui:WaitForChild("LoadingScreenBackground")

local logo = loadingScreenBG:WaitForChild("Logo")

local loadingBarBG = loadingScreenBG:WaitForChild("LoadingBarBG")
local clippingFrame = loadingBarBG:WaitForChild("ClippingFrame")
local loadingBar = clippingFrame:WaitForChild("LoadingBar")


clippingFrame.Size = UDim2.new(0, 0, 1, 0)

clippingFrame:TweenSize(UDim2.new(1, 0, 1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 5)


clippingFrame:GetPropertyChangedSignal("Size"):Connect(function()	
	
	loadingBar.Size = UDim2.new(1/clippingFrame.Size.X.Scale, 0, 1, 0)
end)


repeat wait() until clippingFrame.Size == UDim2.new(1, 0, 1, 0)


local loadingScreenFade = tweenService:Create(loadingScreenBG, TweenInfo.new(3, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut), {BackgroundTransparency = 1})
loadingScreenFade:Play()

logo:TweenPosition(UDim2.new(0.5, 0, -0.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 2)
loadingBarBG:TweenPosition(UDim2.new(0.5, 0, 1.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 2)



local originalCFrame = camera.CFrame
local scaleFactor = 1000


game:GetService("RunService").RenderStepped:Connect(function()
	
	
	local centreOfScreen = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)
	
	local mouseDistanceFromCentre = Vector3.new((-mouse.X - centreOfScreen.X) / scaleFactor, (mouse.Y - centreOfScreen.Y) / scaleFactor, 0)
	
	
	camera.CFrame = originalCFrame * CFrame.new(originalCFrame.LookVector + mouseDistanceFromCentre)
end)


local playButtonClicked = false

playBtn.MouseButton1Click:Connect(function()
	
	if playButtonClicked then return end
	playButtonClicked = true
	
	
	creditsBG:TweenPosition(UDim2.new(-0.5, 0, 0.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 1, true)
	
	mainMenuBG:TweenPosition(UDim2.new(-1, 0, 0, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 3, true)
	
	
	wait(3)
	
	char.HumanoidRootPart.Anchored = false
	char.HumanoidRootPart.CFrame = workspace.MenuComponents.SpawnPart.CFrame
	
	camera.CameraType = Enum.CameraType.Custom
	
	mainMenuGui:Destroy()
	
	wait(1)
	plr.PlayerGui.PlotSelects.Disabled = false
	plr.PlayerGui.BadgeButton.ImageButton.Visible = true
	plr.PlayerGui.CodeButton.ImageButton.Visible = true
	plr.PlayerGui.InventoryButton.ImageButton.Visible = true
	plr.PlayerGui.QuestButton.ImageButton.Visible = true
	plr.PlayerGui.SettingButton.ImageButton.Visible = true
	plr.PlayerGui.ShopButton.ImageButton.Visible = true
	plr.PlayerGui.Cash.ImageLabel.Visible = true
	plr.PlayerGui.PlotSelect.Frame.Visible = true
end)


local creditsOpen = false

creditsBtn.MouseButton1Click:Connect(function()
	
	
	if playButtonClicked then return end
	
	
	if creditsOpen then
		creditsBG:TweenPosition(UDim2.new(-0.5, 0, 0.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 1, true)
		
	else
		creditsBG:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 1, true)
	end
	
	creditsOpen = not creditsOpen
end)


local function hoverOnButton(btn)
	
	if playButtonClicked then return end
	
	local colourDarken = tweenService:Create(btn, TweenInfo.new(0.1, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut), {ImageColor3 = Color3.fromRGB(221, 221, 221)})
	colourDarken:Play()
end

local function hoverOffButton(btn)
	
	if playButtonClicked then return end
	
	local colourNormal = tweenService:Create(btn, TweenInfo.new(0.1, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut), {ImageColor3 = Color3.fromRGB(255, 255, 255)})
	colourNormal:Play()
end


playBtn.MouseEnter:Connect(function()
	
	hoverOnButton(playBtn)
end)
creditsBtn.MouseEnter:Connect(function()
	
	hoverOnButton(creditsBtn)
end)

playBtn.MouseLeave:Connect(function()
	
	hoverOffButton(playBtn)
end)
creditsBtn.MouseLeave:Connect(function()
	
	hoverOffButton(creditsBtn)
end)

should i put script in a screenGui and turn the ResetOnSpawn off?

No. I recommend you put it in StarterPlayer → StarterPlayerScripts.

You should note that the script will not refresh this way, so variables will not always be up-to-date when referring to ScreenGUIs.

To solve this, connect a function to the player’s CharacterAdded (which fires when they spawn) and update the variables accordingly, as you see fit.

2 Likes