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

How to make this script with maps work?

Asked by 10 years ago

This is the same script I showed you before, but with maps on it. I put more maps in it and added it on the script, but it didn't spawn me anywhere. Hope you understand what I am saying. This is the script:

local checkpoint1 = game.Workspace.Checkpoint1
local checkpoint2 = game.Workspace.Checkpoint2
local checkpoint3 = game.Workspace.Checkpoint3

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

local raceInProgress = false

local lobbySpawn = game.Workspace.LobbySpawn
local trackSpawn = game.Workspace.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)

local serverstorage = game:GetService("ServerStorage")
local replicatedstorage = game:GetService("ReplicatedStorage")
local event = replicatedstorage:WaitForChild("RemoteEvent")
local maps = serverstorage:WaitForChild("Maps")
local mapholder = game.Workspace:WaitForChild("MapHolder")
local statustag = replicatedstorage:WaitForChild("StatusTag")
local timertag = replicatedstorage:WaitForChild("TimerTag")

while true do
    --load a random map
    statustag.Value = "Loading Map"
    timertag.Value = -1
    mapholder:ClearAllChildren()
    wait(2)
    local allmaps = maps:GetChildren()
    local newmap = allmaps[math.random(1, #allmaps)]:clone()
    newmap.Parent = mapholder
    wait(2)
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)

    --teleport to map
    statustag.Value = "Loading Map"
    timertag.Value = -1
    mapholder:ClearAllChildren()
    wait(2)
    local allmaps = maps:GetChildren()
    local newmap = allmaps[math.random(1, #allmaps)]:clone()
    newmap.Parent = mapholder
    wait(2)
end

1 answer

Log in to vote
0
Answered by 10 years ago

Looking at your code, it might be the function at Line 133;

function teleportPlayers(target) --Line 133
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

I'm just guessing, but it may be the while true do end loop you are using, you are also attempting to use GetChildren for game.Players, if there was a Hat in Players and you used GetChildren, as well as checking for the character, the script may break or something like that, let's try using GetPlayers :P Let's go ahead and fix your code;

function teleportPlayers(target) --Line 133
for _, player in pairs(game.Players:GetPlayers()) do --Let's try using `GetPlayers` instead :P
while not player.Character do --Doesn't this look so much better? :)
wait()
if player.Character then break end --Just-in case :)
end
local character = player.Character
local torso = character:WaitForChild("Torso")
torso:MoveTo(target.Position) --We are using the ':MoveTo' method instead, for a hopefully better efficiency for the code :)
end
end

If this does not help, or the code is still not working, just let me know and I'll take a look at it again. :) And it would be very helpful if you had any Output. :) Hope this helped!

Ad

Answer this question