✓ Accepted answer
Diagnosis
There are many things wrong with this code, both logically, and visibly. I'll try and point them all out at once here, and go over it as I explain.
Problems
ServerStorage
- For one, you can't work with the ServerStorage service inside a local script (as Ryukiyo mentioned). This service is exclusively for the server to access, which leaves you with the choice of either using a remote event / function, or using a server script. OR you could use the ReplicatedStorage service.
Getting the character
- While this may not cause a problem 100% of the time, it sure is a weak spot. Depending on where your local script is and what's loading first, your script could potentially just stop and error right there for trying to get the character before it loads. This is a common problem that can simply be fixed by using an or statement, followed by a wait method on the CharacterAdded event. Here's an example:
local Char = player.Character or player.CharacterAdded:wait()
If you wanna know more about why this works, just let me know.
Variables
- Another thing, you created a variable that represents the local player. So, why are you not using it on line 8? And why are you indexing the Players service for the local player's name if you could just use LocalPlayer again? You also did this again on line 11.
Getting the Humanoid
- Same thing here with the Character. You don't know if the Humanoid will always be there and ready to use when the script is, so use WaitForChild.
While loop
- That while loop is pretty unecessary, since your event only fires when the player dies, so there's no need to keep checking if the player is dead. Same goes for the if statement.
Conclusion
All together, if you fix all these issues and place everything in the right location, you should get something like this:
local storage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local Char = player.Character or player.CharacterAdded:wait()
local Human = Char:WaitForChild("Humanoid")
Human.Died:connect(function()
local gui = storage:FindFirstChild("Main")
if gui then
gui:Clone().Parent = playerGui
end
end)
Hope that helped - let me know if you have any questions.
2 comments
all you did was set up a variable...how would that help in any case...
The first two lines are still invalid. You cannot access ServerStorage children in a local script.