Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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.

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. mrmikov999 22 — 3y
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: Vinceberget 1420 — 3y
0
game.Lighting:FindFirstChild("NearDeathEfffect"):Destroy() Vinceberget 1420 — 3y
0
Yea, but I made it tween to make it smooth, not just instantly disappear. mrmikov999 22 — 3y
View all comments (3 more)
0
It won't. The GUI will only be removed once the player respawns if you put it at the top of the script. Vinceberget 1420 — 3y
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. mrmikov999 22 — 3y
0
Well whatever, still kinda solves the issue of breaking the script. Thanks for helping. mrmikov999 22 — 3y
Ad

Answer this question