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

Activating GUI with a brick?

Asked by 9 years ago

So, In my game I have some levels, each with a brick called "End". The following code is used:

function onTouch()
    print ("End Active")
    game.ServerStorage.Check.Value = "1"
    wait (6)
    game.ServerStorage.Check.Value = "0"
end

script.Parent.Touched:connect(onTouch)

"check" is a NumberValue. In my GUI, I have the following code:

script.Parent.Position = UDim2.new(0.5, 0, 0.5, 0) -- We set the scales to .5, .5, which is the center of the screen
script.Parent.Size = UDim2.new(0,0,0,0) -- We set the frame size too 0, so it will pop out of the middle of the screen.  
local Value = game.ServerStorage.Check.Value

if (Value == "1") then
    print ("Hello World!")
    script.Parent.Visible = true    
    script.Parent:TweenSizeAndPosition(UDim2.new(0.3, 0, 0.1, 0), UDim2.new(.35, 0, .47, 0), "Out", "Quad", 1) 
    script.Parent.TextScaled = true
    wait (3)
    local torso = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Character.Torso
    local destination = script.Parent.TeleportDestination.Value
    torso.CFrame = CFrame.new(destination,torso.CFrame.lookVector)
    print ("Player Teleported. Map Loading Complete. Removing GUI")
    wait (2)
    script.Parent.Visible = false
    script.Parent.Parent.Main.lvlSelect.Visible = true
end

Script.Parent is a TextLable that I want to appear at the end of a level. I've got no clue what's wrong. I've had the 'if' statement work and had the code afterwards work in a different script. Both of the scripts are in Scripts and not LocalScripts, so any advice?

1 answer

Log in to vote
0
Answered by 9 years ago

Firstly, don't put a property of an object into a variable. You can use the property, but the variable saves a local version of the value and the variable doesn't update when the property changes.

Secondly, your code will only run once and you will need to connect your code to an event such as the Changed event in order for it to keep checking if the level is complete.

Thirdly, since the object is a NumberValue, you don't need the speech marks around the 1, otherwise you'll be trying to find a string instead of a number, the if statement will not be true and the code won't run after it.

So let's first get rid of the .Value from the variable and then reference it in the if statement. We will also get rid of the speech marks to make it check for a number, not a string.

script.Parent.Position = UDim2.new(0.5, 0, 0.5, 0) -- We set the scales to .5, .5, which is the center of the screen
script.Parent.Size = UDim2.new(0,0,0,0) -- We set the frame size too 0, so it will pop out of the middle of the screen.  
local Value = game.ServerStorage.Check --Variables save the value of the property when you set a variable as a property, so the variable won't update when it changes.

if (Value.Value == 1) then --If the Value object's value equals 1 then continue with the code.
    print ("Hello World!")
    script.Parent.Visible = true    
    script.Parent:TweenSizeAndPosition(UDim2.new(0.3, 0, 0.1, 0), UDim2.new(.35, 0, .47, 0), "Out", "Quad", 1) 
    script.Parent.TextScaled = true
    wait (3)
    local torso = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Character.Torso
    local destination = script.Parent.TeleportDestination.Value
    torso.CFrame = CFrame.new(destination,torso.CFrame.lookVector)
    print ("Player Teleported. Map Loading Complete. Removing GUI")
    wait (2)
    script.Parent.Visible = false
    script.Parent.Parent.Main.lvlSelect.Visible = true
end

Now, we need to connect the Changed event to your code. You can use anonymous functions to do this.

The final script should look like this:

script.Parent.Position = UDim2.new(0.5, 0, 0.5, 0) -- We set the scales to .5, .5, which is the center of the screen
script.Parent.Size = UDim2.new(0,0,0,0) -- We set the frame size too 0, so it will pop out of the middle of the screen.  
local Value = game.ServerStorage.Check --Variables save the value of the property when you set a variable as a property, so the variable won't update when it changes.

Value.Changed:connect(function() --Wrap a function in the connection line. This is called an anonymous function.
    if (Value.Value == 1) then --If the Value object's value equals "1" then continue with the code.
        print ("Hello World!")
        script.Parent.Visible = true    
        script.Parent:TweenSizeAndPosition(UDim2.new(0.3, 0, 0.1, 0), UDim2.new(.35, 0, .47, 0), "Out", "Quad", 1) 
        script.Parent.TextScaled = true
        wait (3)
        local torso = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Character.Torso
        local destination = script.Parent.TeleportDestination.Value
        torso.CFrame = CFrame.new(destination,torso.CFrame.lookVector)
        print ("Player Teleported. Map Loading Complete. Removing GUI")
        wait (2)
        script.Parent.Visible = false
        script.Parent.Parent.Main.lvlSelect.Visible = true
    end
end) --Closing parenthesis to correctly wrap the function into the Changed event's connection.

I hope my answer helped you. If it did, be sure to accept it.

0
It did help me. A lot. Thanks! awsomespacegeek 20 — 9y
Ad

Answer this question