local serverstorage = game:GetService("ServerStorage") local player = game.Players.LocalPlayer local Char = player.Character Char.Humanoid.Died:connect(function() while true do wait() game.Players[player.Name]:WaitForChild("FoodAmount").Value = 0 if Char.Humanoid.Health > 0 then wait() serverstorage.Main:Clone().Parent = game.Players[player.Name].PlayerGui --serverstorage.Main is the GUI I am cloning into the player, doesn't work? :( break end end end)
When the player died, everything works except the Gui isnt cloned into the player, help please? thanks
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.
ServerStorage
Getting the character
local Char = player.Character or player.CharacterAdded:wait()
If you wanna know more about why this works, just let me know.
Variables
Getting the Humanoid
While loop
All together, if you fix all these issues and place everything in the right location, you should get something like this:
-- I'm just using ReplicatedStorage, cause that'll save a lot of time and energy local storage = game:GetService("ReplicatedStorage") local player = game.Players.LocalPlayer -- Also creating a variable for the PlayerGui, just cause it's neater. Using WaitForChild here is also necessary most of the time. local playerGui = player:WaitForChild("PlayerGui") -- Using the CharacterAdded:wait() method with the or statement. local Char = player.Character or player.CharacterAdded:wait() -- Using WaitForChild to load the Humanoid. local Human = Char:WaitForChild("Humanoid") -- Connect the event Human.Died:connect(function() -- Find the GUI local gui = storage:FindFirstChild("Main") -- Make sure it exists if gui then gui:Clone().Parent = playerGui -- Clone it end end)
Hope that helped - let me know if you have any questions.
It seems you are trying to access your game's ServerStorage
through a local script; you cannot do this due to the restrictions of local scripts.
Consider using a regular script or remote event to connect to the ServerStorage
service. If you need any examples, let me know.
See if this works
local serverstorage = game:GetService("ServerStorage") local player = game.Players.LocalPlayer local Char = player.Character local TheGui=serverstorage.Main Char.Humanoid.Died:connect(function() while true do wait() game.Players[player.Name]:WaitForChild("FoodAmount").Value = 0 if Char.Humanoid.Health > 0 then wait() TheGui:Clone().Parent = game.Players[player.Name].PlayerGui --[[ Also you can just do "TheGui:Clone().Parent = player.PlayerGui" aswell. ]] break end end end)