Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script doesn't work whenever the player respawns, any clues on how to solve the issue?

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)

1 answer

✓ Accepted answer

The problem is that it only fires the event once the original humanoid dies. Everytime a character respawns, a new humanoid and character is created.

You should be able to just place the script in StarterCharacterScripts to fix the issue.

7 comments

0

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.

0

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:

0

game.Lighting:FindFirstChild("NearDeathEfffect"):Destroy()

0

Yea, but I made it tween to make it smooth, not just instantly disappear.

0

It won't. The GUI will only be removed once the player respawns if you put it at the top of the script.

0

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.

0

Well whatever, still kinda solves the issue of breaking the script. Thanks for helping.

This discussion is preserved as it was. New answers, comments, and votes are closed.