Ive been trying to make a death screen for awhile now, and this is the last thing i need to do. I want to make it so that when you die, A gui will fade in that will say "You died" I have the "You died" part done, but I need to know how to make it fade in.
I am really bad at scripting so this is all i could come up with, but ofc it doesnt work
game.Players.LocalPlayer.Character:WaitForChild('Humanoid').Died:connect(function() script.Parent.ImageTransparency = 1 wait(0.1) script.Parent.ImageTransparency = 0.9 wait(0.1) script.Parent.ImageTransparency = 0.8 wait(0.1) script.Parent.ImageTransparency = 0.7 wait(0.1) script.Parent.ImageTransparency = 0.6 wait(0.1) script.Parent.ImageTransparency = 0.5 wait(0.1) script.Parent.ImageTransparency = 0.4 wait(0.1) script.Parent.ImageTransparency = 0.3 wait(0.1) script.Parent.ImageTransparency = 0.2 wait(0.1) script.Parent.ImageTransparency = 0.1 wait(0.1) script.Parent.ImageTransparency = 0 wait(0.1) end)
Nice try, you did get almost everything right, but the reason this is not working is maybe because you were refering to game.Players.LocalPlayer
in a server script (a normal script) which you can't do. You have to do it in a loca script, that also applies for GUIs, I'm assuming this server script is parented to a UI (script.Parent
), which won't work out as well, UIs need to be controlled from a local script, because they are local or in other words client-sided, meaning they are handled from the player's computer (also known as the client) and not the server.
So change this to a local script, and another thing to point out is, this part
script.Parent.ImageTransparency = 1 wait(0.1) script.Parent.ImageTransparency = 0.9 wait(0.1) script.Parent.ImageTransparency = 0.8 wait(0.1) script.Parent.ImageTransparency = 0.7 wait(0.1) script.Parent.ImageTransparency = 0.6 wait(0.1) script.Parent.ImageTransparency = 0.5 wait(0.1) script.Parent.ImageTransparency = 0.4 wait(0.1) script.Parent.ImageTransparency = 0.3 wait(0.1) script.Parent.ImageTransparency = 0.2 wait(0.1) script.Parent.ImageTransparency = 0.1 wait(0.1) script.Parent.ImageTransparency = 0 wait(0.1)
Logically speaking, it should work as you'd think it would, but there is a much more efficient way to do it that offers more readability, using for loops. For loops are basically loops that loop a certain amount of times.
for i = 0, 10, 1 do print(i) end
This loop will loop from 0 to 10, going up by 1 each loop. i
is the value the loop is currently at. So it would print 0 1 2 3 4 5 6 7 8 9 10.
for i = 0, 14, 2 do print(i) end
This would go from 0 to 14 going up by 2, so it would technically loop 7 times.
We can utilise this, we can go from 0 (invisible) to 1 (visible), by 0.1 for example so it does it slowly, and we set the transparency value to i
, because represents the value that we are making go from 0 to 1. Think a bit about what we did, it's pretty cool.
game.Players.LocalPlayer.Character:WaitForChild('Humanoid').Died:connect(function() for i = 0, 1, 0.1 do wait(0.1) --we add a wait so it doesn't finish the loop instanly script.Parent.Transparency = i end end)
And just in case you are wondering what :WaitForChild()
is, it basically waits for an object to load, in this case if humanoid didn't load yet it would wait for it.
here try this:
game.Players.LocalPlayer.Character:WaitForChild('Humanoid').HealthChanged:Connect(function(health) if health <= 0 then script.Parent.Visible = true script.Parent.ImageTransparency = 0 for i =1,10 do script.Parent.ImageTransparency = script.Parent.ImageTransparency -0.1 wait(0.1) end end end)
tell me if theres any errors still and ill help out