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

?Map changger i cant figure out what this error means at all

Asked by 7 years ago

Ok so i made a map changer script but i cant figure out what the error is what i need to do he is the error 12:17:12.599 - Players.Player1.PlayerGui.InGameGui.Frame.TextBox.LocalScript:20: attempt to index local 'map' (a nil value)

while wait() do
    if script.Parent.Parent.Ended.Value == 1 then 

for i = 30,0, - 1 do
    local Text = script.Parent
    local Frame = script.Parent.Parent 
    Text.Text = i
    wait(1)
    if i == 0 then

        script.Parent.Parent.Ended.Value = 0
        spawnmap = game.Workspace.Spawnmap
        spawnmap.Value = 1 
        Text.Visible = false
        Frame.TextBox2.Text = "ROUND STARTING"
        wait(3)
        Text.Visible = false
        Frame.TextBox2.Text = "12 HOURS TELL DAY"
        local map = game.Workspace:FindFirstChild("Map"..spawnmap.Value)
        local target = map.thing.Position
        Players = game.Players:GetPlayers()
        local player = game.Players.LocalPlayer
        Num = Instance.new("IntValue",player)
        Num = math.random(1,#Players)
        if Num == 1 then
            player.PlayerGui.IngameGui.BackGround.Visible = true
        end
        if player.Shop.Value == 0 then
        player.Character:MoveTo(target)
    Min = 30
    Sec1 = 0
    Sec  = 0 -- both should start at 0 
    Text.Text = Min..":"..Sec1..Sec
    for i = 600,0,-1 do
        wait(1)
        print(i)
        if i == 600 then
            Min = 9
            Sec1 = 5
            Sec = 9
        Text.Text = Min..":"..Sec1..Sec
        elseif i == 540 then
            Min = 8
            Sec1 = 5
            Sec = 9
        Text.Text = Min..":"..Sec1..Sec
        elseif i == 480 then
            Min = 7
            Sec1 = 5
            Sec = 9
        Text.Text = Min..":"..Sec1..Sec
        elseif i == 420 then
            Min = 6
            Sec1 = 5
            Sec = 9
        Text.Text = Min..":"..Sec1..Sec
        elseif i == 360 then
            Min = 5
            Sec1 = 5
            Sec = 9
        Text.Text = Min..":"..Sec1..Sec
        elseif i == 300 then
            Min = 4
            Sec1 = 5
            Sec = 9
        Text.Text = Min..":"..Sec1..Sec
        elseif i == 240 then
            Min = 3
            Sec1 = 5
            Sec = 9
        Text.Text = Min..":"..Sec1..Sec
        elseif i == 180 then
            Min = 2 
            Sec1 = 5
            Sec = 9 
        Text.Text = Min..":"..Sec1..Sec
        elseif i == 120 then
            Min = 1
            Sec1 = 5
            Sec = 9
        Text.Text = Min..":"..Sec1..Sec
        elseif i == 60 then
            Min = 0
            Sec1 = 5
            Sec = 9
        Text.Text = Min..":"..Sec1..Sec     
         else
            Sec = Sec - 1 
            if Sec == -1 then
                Sec1 = Sec1 - 1 
                Sec = 9

            end
        Text.Text = Min..":"..Sec1..Sec
        end


    end

    end

Num:Destroy()
        end
0
you haven't defined the variable 'map' Quackshire 17 — 7y
0
Try using WaitForChild("") pluginfactory 463 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

When you see an error message attempt to index local 'map' (a nil value) (where 'map' can be replaced by any variable name), this means that you have an unexpected nil value. That is, you're telling the script on line 20 to get "map.thing.Position", which requires that 'map' and 'map.thing' are both non-nil -- yet, the error is telling you, 'map' is nil. It is illegal to try and index a nil value (that is, attempt to do "nil.thing" or "nil.Value" or "nil.Anything"), hence the error.

So we go to the previous lines. Line 19 is where you try and assign 'map' a value. How might 'map' be nil there? If "workspace.Map1" does not exist (since 'spawnmap.Value = 1' as of line 13).

Some other problems with your script:

  • Indentation. After every 'if', 'for', 'while', 'function' (anything that opens a 'block' and must be ended with 'end'), you should have one more indentation than before until you get to the corresponding 'end'. ex:
function example()
    randomCode()
    if condition then
        print("hi")
    elseif otherCondition then
        doSomething()
    else
        doSomethingElse()
    end
    for i = 1, 2 do
        for j = 100, 200 do
            print(i, j, i+j)
        end
    end
end
  • On line 34 you start a new for loop using the same variable name you use on line 4. This hides the 'i' declared on line 4, meaning you cannot access it in your inner for loop. You should use a different variable name (ex, j) so that you access both for-loop variables.
  • Lines 37-94 are very similar, which almost always means there's a better way to do it. In this case, math and string.format will suffice; the following bit replaces lines 34 to 98:
for timeLeft = 599, 0, -1 do
    print(i)
    Text.Text = string.format("%d:d", math.floor(timeLeft / 60), timeLeft % 60)
    wait(1)
end

Changes:

  • Start at 599 and you will wait for exactly 10 minutes rather than 10 minutes and one second. Further, the first time displayed will be 9:59, just like you had originally.
  • Look up string.format in lua's String Library Tutorial and/or experiment with it yourself. It returns the same string you give it as the first argument, but with %d replaced with the number you give it as an argument (and it can accept any number of arguments). The d means "Always show two digits even if the number is less than 10".
  • math.floor(timeLeft / 60) gets the number of minutes left
  • timeLeft % 60 gets rid of the minutes so that you're just left with seconds (% 60 is sort of like subtracting 60 repeatedly until the value is 59 or less)
  • I put the wait(1) after the Text.Text so that the 0:00 part will display for a full second. Further, 9:59 will show up immediately rather than after a full second of waiting.
Ad

Answer this question