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

My gui isn't becoming visible when I tell it to?

Asked by
Djinous 45
8 years ago

I have a script that for every time a player respawns after the first time, this gui will become visible so they can interact with it, but although I'm sure it's correct, it's not showing up. What am I doing wrong? The vector is supposed to be empty, I don't have a vector for it yet, I can't use it if I can't see the gui.

local debounce = false
function CharacterAdded(player)
    if debounce == true then
        game.StarterGui.ScreenGui.SpawnGui.Visible = true
    else
        debounce = true
        end
function Click(mouse)
    if game.Workspace.ERDSObj.Marker.Color == script.Parent.Parent.Parent.Parent.Parent.TeamColor then
        script.Parent.Parent.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(Vector3.new())
        game.StarterGui.ScreenGui.SpawnGui.Visible = false
    end
end
end

script.Parent.MouseButton1Down:connect(Click)

1 answer

Log in to vote
1
Answered by 8 years ago

You have a few issues:

  • You're never connecting your CharacterAdded function to any events
  • You're attempting to connect MouseButton1Down to Click, which is only defined inside of a function which is never called. If you're not sure, that means Click is nil when you try to connect it.
  • You're trying to set a property of a member of StarterGui. That means that the Player will never notice these changes until they respawn.

CharacterAdded

You need to connect your CharacterAdded function to the Player.CharacterAdded event. That's easy enough once you have the Player in question. Because your script is in a PlayerGui, just keep going up until you reach it.

MouseButton1Down

You need to move your Click function outside of the CharacterAdded function. That's easy enough, given that you don't need the Character in question for your code to work.

StarterGui

Don't change stuff in StarterGui. Never change stuff in the StarterGui. You need to change stuff in the Player.PlayerGui, which is fine for you because you can do it all relative to script or Player - Both of which are available to you from the get-go.


In all, your fixed code should look something like:

local Player = script;
repeat Player = Player.Parent until Player.Parent == game.Players
-- That allows us to get the Player with no regard to the hierarchy.

local Character = Player.Character or Player.CharacterAdded:wait();
-- That allows us to get the Character.

if not Player.Respawned.Value then -- We'll sort this out in a second.
    Player.PlayerGui.ScreenGui.SpawnGui.Visible = true
end;
function Click(mouse)
    if game.Workspace.ERDSObj.Marker.Color == Player.TeamColor then
    Player.Character.Torso.CFrame = CFrame.new(0,0,0)
        Player.PlayerGui.SpawnGui.Visible = false
    end
end
script.Parent.MouseButton1Down:connect(Click)

And then in another script, because of how your dodgy code is handling itself, you'll need a script that tells the script whether the Player has respawned.

game.Players.PlayerAdded:connect(function(p)
local i = Instance.new("BoolValue");
i.Name = "Respawned";
i.Value = false;
i.Parent = p;
p.CharacterAdded:wait():WaitForChild("Humanoid").Died:wait();
i.Value = true
end);
Ad

Answer this question