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

Why won't the GUI be placed in the player?

Asked by 8 years ago
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

3 answers

Log in to vote
2
Answered by 8 years ago

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:

-- 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.

Ad
Log in to vote
0
Answered by
Ryukiyo 65
8 years ago

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.

Log in to vote
-2
Answered by
Dr_Doge 100
8 years ago

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)

0
all you did was set up a variable...how would that help in any case... thehybrid576 294 — 8y
0
The first two lines are still invalid. You cannot access ServerStorage children in a local script. ScriptGuider 5640 — 8y

Answer this question