So, when my guy dies, I want there to be a screen that says "you died". I tried making this, and it wouldn't work. Here is my code, with a lot of notes.
game:GetService('Players').PlayerAdded:connect(function(player) --getting service player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() -- checks for death print(player.Name .. " has died!") local f = script.Parent.Parent --variable local txt = script.Parent --variable f.BackgroundTransparency = 1 txt.BackgroundTransparency = 1 txt.TextTransparency = 1 local i = 0 --variable while wait(0.01) do --fade effect start f.BackgroundTransparency = f.BackgroundTransparency - 0.01 --lose transparency txt.BackgroundTransparency = txt.BackgroundTransparency - 0.01 --lose transparency txt.TextTransparency = txt.TextTransparency - 0.01 --lose transparency if txt.BackgroundTransparency == 0.8 then --if at wanted position then f.BackgroundTransparency = 0 txt.TextTransparency = 0 i = "stop" break --ends the script end --if not at wanted position, then retry end end) end) end) while wait() do --check if death screen should end if i == "stop" then wait(5) i = 0 local f = script.Parent.Parent -- renaming variable local txt = script.Parent -- renaming variable txt.BackgroundTransparency = 1 f.BackgroundTransparency = 1 txt.TextTransparency = 1 end end
So, that's basically my code. When I check the output, there are no errors. Please help me out!
Before I start: If you're doing any sort of interpolation, you will need to learn about TweenService. Read up on it here, as it's what I used to retrofit your code.
What happens in this code is, just like you had it, a function is connected to a player dying. However, I replaced of all the complex fading you were attempting with, again, TweenService. (Have I mentioned you should learn it? ;) ). One section, the top, is for fading everything in. The lower section is for fading it out after a little bit. It's all simple once you learn how to do it. Also, a tip about comments: Don't just label what somthing is, label what it does. Doing that with your comments will help you in the long run. Good luck!
local player = game.Players.LocalPlayer --Player that is owner of the localScript local tweenService = game:GetService("TweenService") local f = script.Parent.Parent --variable local txt = script.Parent --variable --Initialize Transperencys-- f.BackgroundTransparency = 1 txt.BackgroundTransparency = 1 txt.TextTransparency = 1 --Changeable Variables-- local timeForFade = 1 --How long it takes for fade in and out local wantedTransparencyOnDeath = 0 --Transparency of the GUI on death local normalTransparency = 1 --Transparency normally local timeBeforeGuiFadesAgain = 5 --Delay before fading out function IDied() print(player.Name .. " has died!") local quadTweenInfo = TweenInfo.new(timeForFade, Enum.EasingStyle.Quad, Enum.EasingDirection.Out) local goal = {} goal.BackgroundTransparency = wantedTransparencyOnDeath tweenService:Create(f, quadTweenInfo, goal):Play() tweenService:Create(txt, quadTweenInfo, goal):Play() goal = nil local goal = {} goal.TextTransparency = wantedTransparencyOnDeath tweenService:Create(txt, quadTweenInfo, goal):Play() wait(timeBeforeGuiFadesAgain) local goal = {} goal.BackgroundTransparency = normalTransparency tweenService:Create(f, quadTweenInfo, goal):Play() tweenService:Create(txt, quadTweenInfo, goal):Play() goal = nil local goal = {} goal.TextTransparency = normalTransparency tweenService:Create(txt, quadTweenInfo, goal):Play() end player.Character:WaitForChild("Humanoid").Died:connect(IDied)
If my answer has helped you, make sure to mark it correct :)