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

How do I fix this error and script?

Asked by 9 years ago

I am making a game with maps. But, this script is not spawning me in the track. Plus there was an error with it. Hope you understand what I am saying. I only made two. But I want it to work if I add more maps to it. This is the script and errors:

local checkpoint1 = game.Workspace.MapHolder.Map1Beginner.Checkpoint1
local checkpoint2 = game.Workspace.MapHolder.Map1Beginner.Checkpoint2
local checkpoint3 = game.Workspace.MapHolder.Map1Beginner.Checkpoint3
local checkpoint1 = game.Workspace.MapHolder.Map2Turning.Checkpoint1
local checkpoint2 = game.Workspace.MapHolder.Map2Turning.Checkpoint2
local checkpoint3 = game.Workspace.MapHolder.Map2Turning.Checkpoint3

local redLight = game.Workspace.MapHolder.Map1Beginner.StartLight.RedModel.LightBulb.PointLight
local yellowLight = game.Workspace.MapHolder.Map1Beginner.StartLight.RedModel.LightBulb.PointLight
local greenLight = game.Workspace.MapHolder.Map1Beginner.StartLight.RedModel.LightBulb.PointLight
local startBarrier = game.Workspace.MapHolder.Map1Beginner.StartBarrier
local redLight = game.Workspace.MapHolder.Map2Turning.StartLight.RedModel.LightBulb.PointLight
local yellowLight = game.Workspace.MapHolder.Map2Turning.StartLight.YellowModel.LightBulb.PointLight
local greenLight = game.Workspace.MapHolder.Map2Turning.StartLight.GreenModel.LightBulb.PointLight
local startBarrier = game.Workspace.MapHolder.Map2Turning.StartBarrier

local raceInProgress = false

local lobbySpawn = game.Workspace.LobbySpawn
local trackSpawn = game.Workspace.MapHolder.Map1Beginner.TrackSpawn
local lobbySpawn = game.Workspace.LobbySpawn
local trackSpawn = game.Workspace.MapHolder.Map2Turning.TrackSpawn

local minimumPlayers = 2

function destroyCars()
    for _, object in pairs(game.Workspace:GetChildren()) do
        print(object.Name)
        if object.Name == "RaceCar" then
            print("This is the RC. Destroy it!")
            object:Destroy()
        end
    end
end

function createCars()
    for _, object in pairs(game.ServerStorage:GetChildren()) do
        local carCopy = object:Clone()
        carCopy.Parent = game.Workspace
        carCopy:MakeJoints()
    end 
end


function showVictoryMessage(playerName)
    local message = Instance.new("Message")
    message.Text = playerName .. " has won!"
    message.Parent = game.Workspace
    wait(2)
    message:Destroy()
end

function checkpoint1hit(otherPart)
    print("Checkpoint 1")
    print(otherPart.Name)
    if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.Parent:FindFirstChild("Humanoid") then
        print("Someone hit me!")
        if not checkpoint1:FindFirstChild(otherPart.Parent.Name) then
            local playerTag = Instance.new("StringValue")
            playerTag.Parent = checkpoint1
            playerTag.Name = otherPart.Parent.Name
        end

        if checkpoint3:FindFirstChild(otherPart.Parent.Name) and raceInProgress then
            print("Player wins!")
            raceInProgress = false
            showVictoryMessage(otherPart.Parent.Name)
        end
    end
end

function checkpoint2hit(otherPart)
    print("Checkpoint 2")
    print(otherPart.Name)
    if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.Parent:FindFirstChild("Humanoid") then
        print("Someone hit me!")
        if not checkpoint2:FindFirstChild(otherPart.Parent.Name) and checkpoint1:FindFirstChild(otherPart.Parent.Name) then
            local playerTag = Instance.new("StringValue")
            playerTag.Parent = checkpoint2
            playerTag.Name = otherPart.Parent.Name
        end
    end
end

function checkpoint3hit(otherPart)
    print("Checkpoint 3")
    print(otherPart.Name)
    if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.Parent:FindFirstChild("Humanoid") then
        print("Someone hit me!")
        if not checkpoint3:FindFirstChild(otherPart.Parent.Name) and checkpoint2:FindFirstChild(otherPart.Parent.Name) then
            local playerTag = Instance.new("StringValue")
            playerTag.Parent = checkpoint3
            playerTag.Name = otherPart.Parent.Name
        end
    end
end

checkpoint1.Touched:connect(checkpoint1hit)
checkpoint2.Touched:connect(checkpoint2hit)
checkpoint3.Touched:connect(checkpoint3hit)

    function startLightCycle()
    redLight.Enabled = true

    wait(1)

    redLight.Enabled = false
    yellowLight.Enabled = true

    wait(1)

    yellowLight.Enabled = false
    greenLight.Enabled = true
end

function removeBarrier()
    startBarrier.Transparency = 1
    startBarrier.CanCollide = false
end

function resetLights()
    redLight.Enabled = false
    yellowLight.Enabled = false
    greenLight.Enabled = false
end

function resetBarrier()
    startBarrier.Transparency = .5
    startBarrier.CanCollide = true
end

function clearCheckpoint(checkpoint)
    for _, object in pairs(checkpoint:GetChildren()) do
        if object.Name ~= "TouchInterest" then
            object:Destroy()
        end
    end
end


function teleportPlayers(target)
    for _, player in pairs(game.Players:GetChildren()) do
            while player.Character == nil do
                wait()
            end
        local character = player.Character
        local torso = character:WaitForChild("Torso")
        torso.CFrame = target.CFrame
    end
end

wait(10)

Maps = game.ServerStorage.Maps
WaitTime = 120 -- change to amount of seconds
while true do 
for i=0,#Maps do 
Maps[i]:Clone().Parent = workspace 
wait(WaitTime) 
Maps[i]:Destroy() 
wait(5) 
end 
end

while true do
    -- wait for enough players before we start the game
    while game.Players.NumPlayers < minimumPlayers do
        wait()
    end

    -- setup racetrack  
    -- turn off all lights
    resetLights()

    -- reset barrier
    resetBarrier()

    -- create all cars
    createCars()

    -- clear all checkpoints
    clearCheckpoint(checkpoint1)
    clearCheckpoint(checkpoint2)
    clearCheckpoint(checkpoint3)

    -- teleport all players to the racetrack
    teleportPlayers(trackSpawn)

    wait(5)

    -- start race
    -- start light cycle
    startLightCycle()

    -- remove barrier
    removeBarrier()

    raceInProgress = true

    -- wait for race to finish 
    while raceInProgress == true do
        wait()
    end

    wait(2)

    -- delete all cars
    destroyCars()

    wait(2)

    -- teleport all players to the lobby
    teleportPlayers(lobbySpawn)

    wait(10)

end

Output

11:48:43.477 - Use the new http api: no
11:48:51.374 - Started network server on 192.168.1.5|53640
11:48:51.376 - Started network server on 127.0.0.1|53640
11:48:51.840 - Successfully opened file - C:/Users/Window 7/AppData/Local/Roblox/server.rbxl
11:49:01.683 - ServerScriptService.MapScript:157: attempt to get length of global 'Maps' (a userdata value)
11:49:01.684 - Stack Begin
11:49:01.690 - Script 'ServerScriptService.MapScript', Line 157
11:49:01.691 - Stack End
11:49:03.807 - New connection from 127.0.0.1|63084

11:49:04.173 - New connection from 127.0.0.1|63086

Player -1 added
Player -2 added

The error is on line 157 by the way. Please help. Thank you.

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Change line 154 to this:

Maps = game.ServerStorage.Maps:GetChildren()

On line 157, you are treating Maps as a Table, which it isn't. GetChildren() returns a Table of all Children of the Model.

EDIT:

On lines 141 to 150, try changing the function to this:

function teleportPlayers(target)
    for _, player in pairs(game.Players:GetChildren()) do
        if player.Character then
            player.Character:WaitForChild("Torso").CFrame = target.CFrame
            wait()
        end
    end
end
0
So I just change it. User#5689 -1 — 9y
0
There was no error, but it did not spawn me to any of the map. How do I spawn into a random map with 2 players User#5689 -1 — 9y
Ad

Answer this question