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

ScreenGui not showing up?

Asked by 9 years ago

Well I'm making this staircase game, You just climb a bunch of steps and get jumpscared along the way. I made a Instance.new script that makes a ScreenGui and a Textbutton and a ImageButton and a sound that plays a scream. But nothing happens!

Here's the script.

function onTouched()
    skull = Instance.new("ScreenGui")
    skull.Parent = game.StarterGui

    tb = Instance.new("TextButton")
    tb.Size = UDim2.new(0, 1920, 0, 843)
    tb.Parent = skull

    image = Instance.new("ImageButton")
    image.Image = "http://www.roblox.com/asset/?id=24064946"
    image.Parent = tb
    image.Size = UDim2.new(0, 1920, 0, 843)

    scream = Instance.new("Sound", game.Workspace)
    scream.SoundId = 266075228

    wait(2)
    skull:Destroy()
end

script.Parent.Touched:connect(onTouched)

I have no idea what's wrong, I'm a Beginner scripter. Thanks again,

                                                                                   -Lukeisbossman64

2 answers

Log in to vote
1
Answered by 9 years ago

Well what you're doing here is your parenting the screenGui to game.StarterGui.

game.StarterGui only runs once every time the character respawns, so you wont see the changes unless your character respawns. Instead you should detect the player that is touching the part and insert the GUI into their playerGui which is what loads inside of the player instance that holds everything locally on their end. Everything from StarterGui is placed into there when they respawn.

Also you're sound ID needs to be in the correct format: http://www.roblox.com/asset?id=IDHERE

And you need to play the sound with the object's method: soundobject:Play()

I made some changes here that I documented for you so you know what it is doing:

local debounce = false; -- created a debounce so it does not trigger multiple times

function onTouched(part) -- the parameter returned in a touched event is the part that touches
    if game.Players:GetPlayerFromCharacter(part.Parent) and not debounce then -- We check to see if the part that touched can be used to obtain a character from the model. Using GetPlayerFromCharacter we can just ask if part.Parent which can be a character model can obtain a player.

        debounce = true; -- change debounce so it doesn't trigger again while running
        local player = game.Players:GetPlayerFromCharacter(part.Parent); -- we set our player with the same method I just explained above

        skull = Instance.new("ScreenGui")
        skull.Parent = player.PlayerGui -- Place in their PlayerGui

        tb = Instance.new("TextButton")
        tb.Size = UDim2.new(0, 1920, 0, 843)
        tb.Parent = skull

        image = Instance.new("ImageButton")
        image.Image = "http://www.roblox.com/asset/?id=24064946"
        image.Parent = tb
        image.Size = UDim2.new(0, 1920, 0, 843)

        scream = Instance.new("Sound", player.PlayerGui) --Place audio's in the playerGui instead of workspace so the sound is local to the player and can be heard fully by the player as they move forward
        scream.SoundId = "http://www.roblox.com/asset?id=266075228"
        scream:Play();

        coroutine.resume(coroutine.create(function() -- here I create a coroutine that will run along side the rest of your code, that way you don't have to wait an additional 2 seconds. waiting for the audio to end.
            repeat wait(1) until scream.TimePosition == 0 -- we create a repeat loop to wait every second and to break the loop when the audio's TimePosition is reset to 0 which means it would have stopped because the sound had already started from above in very fast miliseconds. 
            scream:Destroy(); -- when it hits 0 meaning it stopped, we destroy it also as you do the screenGui so we don't stack them up 
        end))

        wait(2)
        skull:Destroy()
        debounce = false; -- reset the debounce. 
    end
end

script.Parent.Touched:connect(onTouched)

Ad
Log in to vote
1
Answered by
Discern 1007 Moderation Voter
9 years ago

Your problem is that you are trying to place the GUI inside StarterGui, a place where nobody will see the GUI unless the respawn.

If you want it to appear on their screen right away, you have to place the GUI inside their PlayerGui located inside their Player inside the Players service. You can access this by using the GetPlayerFromCharacter method which returns the player of which the character is associated with. Then access the PlayerGui from there.

Another error is that you are not playing your sound when they touch the part. Just use the Play method and it will play the audio just fine.


Fixed Code:

function onTouched(hit) --Hit is what part touched the script's Parent.
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) --This gets the player from the body part's parent, the character.
    if player ~= nil then --This checks if there actually is a player. If it is, keep going!
        skull = Instance.new("ScreenGui")
        skull.Parent = player.PlayerGui --Parent it inside their PlayerGui.

        tb = Instance.new("TextButton")
        tb.Size = UDim2.new(0, 1920, 0, 843)
        tb.Parent = skull

        image = Instance.new("ImageButton")
        image.Image = "http://www.roblox.com/asset/?id=24064946"
        image.Parent = tb
        image.Size = UDim2.new(0, 1920, 0, 843)

        scream = Instance.new("Sound", game.Workspace)
        scream.SoundId = "http://www.roblox.com/asset?id=266075228" --Set the SoundId in a string and as an asset from the ROBLOX website.
        scream:Play()

        wait(2)
        skull:Destroy()
    end
end

script.Parent.Touched:connect(onTouched)

Here is the Wiki page on the GetPlayerFromCharacter method. Here is the Wiki page on PlayerGui.

If I helped you out, be sure to accept my answer!

0
Ok, thanks, Let me see if this works. Lukeisbossman64 73 — 9y
0
Does not work. Sorry. Lukeisbossman64 73 — 9y
0
Edited. Discern 1007 — 9y

Answer this question