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

Game Script Not Working? [UNSOLVED]

Asked by 9 years ago

I'm trying to make a game script where it picks a random map, creates a message saying the map name, then clones the map to Workspace, deletes the message and respawns all players. But for some reason it won't work I get this output error: attempt to get length of upvalue 'MapDir' (a userdata value). Can anyone tell me what I did wrong?

local MapDir = game.ServerStorage.Maps
local PlayerDir = game.Players
local WaitTime = 10 -- In seconds
local RoundTime = 600 -- In seconds
local Started = false


function GameStart()
MapDir:GetChildren()
for i = 1, #MapDir do
local m = Instance.new('Message', workspace)
m.Text = "Map chosen: "..MapDir[i].Name.." by:"..MapDir[i].CreatedBy.Value..""
wait(WaitTime)
MapDir[i]:Clone().Parent = workspace.MapHolder
PlayerDir:GetChildren()
for i = 1,#PlayerDir do
PlayerDir[i]:LoadCharacter()
Started = true
m:remove()
end
end
end
GameStart()
0
whats the error with if? BSIncorporated 640 — 9y
0
nevermind i think i found the error BSIncorporated 640 — 9y

1 answer

Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
9 years ago
  • Please tab your code correctly. It will be easier to read and will look more professional.
local MapDir = game.ServerStorage.Maps
local PlayerDir = game.Players
local WaitTime = 10 -- In seconds
local RoundTime = 600 -- In seconds
local Started = false

function GameStart()
    MapDir:GetChildren()
    for i = 1, #MapDir do
        local m = Instance.new('Message', workspace)
        m.Text = "Map chosen: "..MapDir[i].Name.." by: "..MapDir[i].CreatedBy.Value..""
        wait(WaitTime)
        MapDir[i]:Clone().Parent = workspace.MapHolder
        PlayerDir:GetChildren()
        for i = 1,#PlayerDir do
            PlayerDir[i]:LoadCharacter()
            Started = true
            m:remove()
        end
    end
end
GameStart()
  • On line 8 and 14, you created a couple of tables.
--[[ Line 8 ]] MapDir:GetChildren()

--[[ Line 14 ]] PlayerDir:GetChildren()

You're on the right track, but this won't work because that's all you did. You didn't set it to a variable or anything. Line 8 will be fixed in the 3rd bullet, but line 14 is the one that needs fixing right here, because the amount of players in the server will change because of players leaving/entering.

--[[ Line 14 ]] local CurrPlayerDir = PlayerDir:GetChildren()
-- Curr = "Current", or updated

--[[ Line 15-19 ]]
for i = 1, #CurrPlayerDir do
    CurrPlayerDir[i]:LoadCharacter()
    Started = true
    m:Destroy() -- Using the ":Destroy()" method is recommended over ":Remove()".
end
  • MapDir is set to the WRONG data type. What the "for" loop on line 9-20 is intended to do is index a table. But MapDir is not a table, it's something else. On line 8, It's trying to get the length of the container of the maps, not regarding the actual THINGS in the container.
--[[ Line 1 ]] local MapDir = game.ServerStorage.Maps:GetChildren()
-- If you did this, then delete line 8.
  • OPTIONAL For convenient purposes, you can just use a generic "for" loop rather than a numeric "for" loop.
-- Numeric "for" loop
TABLE = {}

for i = 1, #TABLE do
    TABLE[]
end
-- Generic "for" loop
TABLE = {}

for i, v in pairs(TABLE) do
    v
end
-- Where "v" means value.

With the generic "for" loop, you get to type a lot less than the former, because the variable for value is a lot shorter than indexing a table (table[key]). But that depends on the purpose of the "for" loop. The generic "for" loop is good for tables.


FONT SIZE TEST

test

hashtag

test

hyphen under the line

test

nothing

0
Question: How do you bullet? Do you have to do alt+codes or is there another way? yumtaste 476 — 9y
0
Plus & space ("+ "). Redbullusa 1580 — 9y
0
Thanks. You wouldn't also happen to know how to put in larger text, would you? I want to make my answers look more professional. yumtaste 476 — 9y
0
Just place 5 of these "-" under the word. woodengop 1134 — 9y
View all comments (3 more)
0
@TheContinentofEurope | Only 1 "-" under a line is sufficient. Redbullusa 1580 — 9y
0
@yumtaste | You could also use a hashtag before each line. "#Hi there." Redbullusa 1580 — 9y
0
Personally, using bullets when regular paragraphs would suffice is annoying. duckwit 1404 — 9y
Ad

Answer this question