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

This door won't move, is it because of the GetRankInGroup?

Asked by 9 years ago

I can't use loops... I put ting so it know if the door is opened or closed... here is the script:

local gate = script.Parent
local ting = 1

game.Players.PlayerAdded(function(plr)

function onClick()
if  plr:GetRankInGroup(947529) >= 9 then
    if ting == 0 then
            ting = 1
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,-.2,0)
            wait(5)
    elseif ting == 1 then
            ting = 0
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(.1)
            gate.Position = gate.Position + Vector3.new(0,.2,0)
            wait(5)
            end
        end
    end
end)

gate.ClickDetector.MouseClick:connect(onClick)
0
Add prints to the script after lines 6, 7, 8, 34, 36 and 62, update the code block in your question with those prints included and tell us where the printing stops in the output. This should give you and us an idea of where the script stops working. Spongocardo 1991 — 9y

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Fixing Problems

Use a for loop to shrink this

First of all, you should not be retyping the same line a number of times. Use a for loop instead.

    for count = 1, 13 do
        -- Repeats 13 times
        gate.Position = gate.Position + Vector3.new(0,.2,0)
        wait(.1)
    end

Now we have a much smaller script:

local gate = script.Parent
local ting = 1

game.Players.PlayerAdded(function(plr)
    function onClick()
        if plr:GetRankInGroup(947529) >= 9 then
            if ting == 0 then
                ting = 1
                for count = 1, 13 do
                    gate.Position = gate.Position + Vector3.new(0,-.2,0)
                    wait(.1)
                end
                wait(5)
            elseif ting == 1 then
                ting = 0
                for count = 1, 13 do
                    gate.Position = gate.Position + Vector3.new(0,.2,0)
                    wait(.1)
                end
                wait(5)
            end
        end
    end
end)

gate.ClickDetector.MouseClick:connect(onClick)

Defining onClick in wrong place

Looking at this, though, we see a problem. onClick isn't defined so that the final connection line can see it -- it's only defined once a Player joins.

Using MouseClick's parameter

So, this needs to be changed. In fact, we don't actually care about which players join. We just need to know the playerWhoClicked

local gate = script.Parent
local ting = 1

function onClick( plr )
    -- Added appropriate `plr` parameter
    if plr:GetRankInGroup(947529) >= 9 then
        if ting == 0 then
            ting = 1
            for count = 1, 13 do
                gate.Position = gate.Position + Vector3.new(0,-.2,0)
                wait(.1)
            end
            wait(5)
        elseif ting == 1 then
            ting = 0
            for count = 1, 13 do
                gate.Position = gate.Position + Vector3.new(0,.2,0)
                wait(.1)
            end
            wait(5)
        end
    end
end
gate.ClickDetector.MouseClick:connect(onClick)

Using .CFrame instead of .Position

And one final tweak. Using Position to move the model is problematic, because it will move out of the way of objects that it might be touching. We should use CFrame instead:

gate.CFrame = gate.CFrame + Vector3.new(0,.2,0)

Improving Style

Unnecessary Wait

The wait(5) that you have in this script is not doing anything -- nothing happens after that in the function! Because it isn't doing anything, it would be better removed. This could help avoid confusion later.

else vs elseif elseif

should probably simply be else since there are only two possibilities, and you always want at least one of them to happen.

Good variable names

ting is a bad variable name. It is meaningless, which means you spend extra time thinking about it. Use something like open and set it to true and false (instead of 0 and 1).

That way, you can use if open then or if not open then which read perfectly in English and make your code clearer!

Option to remove if

One final possibility that might be elegant is to eliminate the if altogether, and use a direction variable of value either 1 or -1.

When you click, you would flip direction by using direction = -direction. When you move the door, you would multiply the Vector3 by direction, a la Vector3.new(0, 0.2, 0) * direction, where -1 corresponds to down and 1 to up.

0
You sir, put a lot of time and effort into your answer. SOMEONE GIVE THIS DUDE REPUTATION! Validark 1580 — 9y
Ad

Answer this question