Hello, so, I have a script that puts a GUI and some effects whenever the player's health is below or equals 15%, it works fine on the launch, but whenever the player respawns, the script stops working. The script is put into StarterPlayerScripts and is a LocalScript. Here's the code:
--vars
local L = game:GetService("Lighting")
local TS = game:GetService("TweenService")
local Plr = game:GetService("Players").LocalPlayer
local CC = Instance.new("ColorCorrectionEffect",L)
CC.Name = "NearDeathEffect"
local gui = Instance.new("ScreenGui",Plr.PlayerGui)
gui.IgnoreGuiInset = true
gui.Name = "NearDeathGUI"
gui.ResetOnSpawn = false
local vignette = Instance.new("ImageLabel",gui)
vignette.Size = UDim2.fromOffset(workspace.CurrentCamera.ViewportSize.X,workspace.CurrentCamera.ViewportSize.Y)
vignette.Image = "http://www.roblox.com/asset/?id=6301559916"
vignette.BackgroundTransparency = 1
vignette.ImageTransparency = 1
vignette.AnchorPoint = Vector2.new(.5,.5)
vignette.Position = UDim2.fromScale(.5,.5)
repeat wait() until Plr.Character
local percent = Plr.Character.Humanoid.Health/100
local neardeath = percent*15
local hum = Plr.Character.Humanoid
local nearDeath = false
hum:GetPropertyChangedSignal("MaxHealth"):Connect(function()
print('max health changed, changing the percent.')
percent = hum.Health/100
neardeath = percent*15
end)
hum:GetPropertyChangedSignal("Health"):Connect(function()
print('changed')
if hum.Health <= neardeath and nearDeath == false then
nearDeath = true
print('near death')
TS:Create(L.NearDeathEffect,TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0),{Brightness = -.3, Saturation = -1}):Play()
TS:Create(Plr.PlayerGui.NearDeathGUI.ImageLabel,TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0),{ImageTransparency = 0}):Play()
elseif hum.Health > neardeath and nearDeath == true then
nearDeath = false
print('regened out')
TS:Create(L.NearDeathEffect,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0),{Brightness = 0, Saturation = 0}):Play()
TS:Create(Plr.PlayerGui.NearDeathGUI.ImageLabel,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0),{ImageTransparency = 1}):Play()
end
end)
hum.Died:Connect(function()
print('died')
TS:Create(L.NearDeathEffect,TweenInfo.new(3,Enum.EasingStyle.Cubic,Enum.EasingDirection.In,0,false,0),{Brightness = -1}):Play()
wait(game:GetService("Players").RespawnTime+1)
nearDeath = false
TS:Create(L.NearDeathEffect,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0),{Brightness = 0, Saturation = 0}):Play()
TS:Create(Plr.PlayerGui.NearDeathGUI.ImageLabel,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0),{ImageTransparency = 1}):Play()
end)

7 comments
I've indeed tried to do that, it partially worked, but it's not the result I needed. After player dies, tweens start playing and after some time it plays more tweens but since the player respawned the script disappeared leaving the properties as they were before the player respawned.
You should try to delete the old gui once the players respawns. This should be as simple as writing this at the top of the script: game.Players.LocalPlayer.PlayerGui:FindFirstChild("NearDeathGUI"):Destroy() as well as this line:
game.Lighting:FindFirstChild("NearDeathEfffect"):Destroy()
Yea, but I made it tween to make it smooth, not just instantly disappear.
It won't. The GUI will only be removed once the player respawns if you put it at the top of the script.
I think either you didn't understand me or I didn't understand you, what I mean is that whenever the player respawns the tweens won't work as they only start after the player respawns, making it unable to play.
Well whatever, still kinda solves the issue of breaking the script. Thanks for helping.