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

Coding Explaination of Why this works and this does not? (Syntax)

Asked by 9 years ago

So I was doing some bug fixing, and realised this worked:

function Click()

    script.Parent.Text = "3"
    wait(1)
    script.Parent.Text = "2"
    wait(1)
    script.Parent.Text = "1"
    wait(1)
    script.Parent.Parent.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(game.Workspace.SpawnLocation.Position) 
    script.Parent.Text = "Spawn"
end

script.Parent.MouseButton1Down:connect(Click)

But this did not:

function Click()
    local  text = script.Parent.Text
     text = "3"
    wait(1)
     text = "2"
    wait(1)
     text = "1"
    wait(1)
    script.Parent.Parent.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(game.Workspace.SpawnLocation.Position) 
     text = "Spawn"
end

script.Parent.MouseButton1Down:connect(Click)

An explanation would be helpful, thanks :)

1 answer

Log in to vote
3
Answered by 9 years ago

When you are setting a variable as a property, the variable stores the value of the property, not the property itself. This means you can't directly edit a property that is set as a variable.

Referencing the property directly or referencing the object from a variable and then referencing the property will change the property, but setting a variable as a property will only save the value of the property and will not let you access the property.

If you want to use a variable, you can just make a variable for the object (in your case, script.Parent) and then use the variable name and reference the property from there.

function Click()
    local label = script.Parent
    label.Text = "3"
    wait(1)
    label.Text = "2"
    wait(1)
    label.Text = "1"
    wait(1)
    script.Parent.Parent.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(game.Workspace.SpawnLocation.Position) 
    label.Text = "Spawn"
end

script.Parent.MouseButton1Down:connect(Click)

To further improve your script, you can use a for loop to make the countdown like this:

function Click()
    local label = script.Parent
    for i = 3, 1, -1 do --The variable i will go from 3 to 1 in an increment of -1.
        label.Text = i --Set the text as the variable i. You don't need to use tostring() here as the number is automatically converted into a string here.
        wait(1) --Wait 1 second.
    end
    script.Parent.Parent.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(game.Workspace.SpawnLocation.Position) 
    label.Text = "Spawn"
end

script.Parent.MouseButton1Down:connect(Click)

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

0
Yep :) Thanks alot ! Bubbles5610 217 — 9y
0
No problem. Spongocardo 1991 — 9y
Ad

Answer this question