|
I'm creating a racing game. I want it to only start the race when there are at least 2 people in the game. I coded it. And when I test it with 2 people it doesn't start the race. I even tried changing the code so only 1 person is required, and it still didn't work. Can someone please help me? here's my code for it.
local MinimumPlayers = 2
while true do
--wait for enough players before we start the game
while game.Players.Numplayers < MinimumPlayers do
wait()
end |
|
|
I hope this isn't the whole script. |
|
|
It's not the whole script for the game. I'll post the whole game's script if neccasery |
|
KEVEKEV77Join Date: 2009-03-12 Post Count: 6961 |
You have to add the code that starts the game..... |
|
|
local Checkpoint1 = game.works##########################################################################################################################lo#####e######## game.workspace.StartLight.Red.PointLight
local Yellowlight = game.workspace.StartLight.Yellow.PointLight
local Greenlight = game.workspace.StartLight.Green.PointLight
local StartBarrier = game.workspace.StartBarrier
local lobbySpawn = game.workspace.LobbySpawn
local TrackSpawn = game.workspace.TrackSpawn
local MinimumPlayers = 2
function Respawncars()
print("I am going to respawn the cars now")
for _, object in pairs(game.ServerStorage:GetChildren()) do
local carCopy = object:Clone()
carCopy.Parent = game.workspace
carCopy:MakeJoints()
end
end
local RaceInProgress = false
function DestroyCars()
for _, object in pairs(game.Workspace:GetChildren()) do
print(object.name)
if object.name == "Car" then
print("This is a car! We must exterminate!")
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 was hit")
print(otherPart.name)
if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.parent:FindFirstChild("Humanoid") then
print("a player 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 was hit")
print(otherPart.name)
if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.parent:FindFirstChild("Humanoid") then
print("a player 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 was hit")
print(otherPart.name)
if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.parent:FindFirstChild("Humanoid") then
print("a player 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()
wa####### #e################ true
wa####### #e################ 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 = 0.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 Cl################## for _, object in pairs(Checkpoint1:GetChildren()) do
if object.Name ~= "TouchInterest" then
object:Destroy()
end
end
end
function teleportPlayers(target)
for _, player in pairs(game.Players:GetChildren()) do
local character = player.Character
local torso = character.HumanoidRootPart
torso.CFrame = target.CFrame
end
end
wait(10)
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()
-- teleport players to Race
teleportPlayers(game.Workspace.TrackSpawn)
-- create more cars
CreateCars()
-- Clear all checkpoints
C#############################C#############################Cl############################-- start race
w########-###t#####i#####ycle
StartLightCycle()
-- remove barrier
removeBarrier()
RaceInProgress = true
-- wait for race to finish
while RaceInProgress == true do
wait()
end
wait (5)
-- delete all cars
DestroyCars()
wait(2)
-- teleport players to lobby
teleportPlayers(lobbySpawn)
wait(10)
end
|
|
KEVEKEV77Join Date: 2009-03-12 Post Count: 6961 |
let me first off tell you the first one you posted SHOULD crash your computer, you didnt put a wait inside the first while loop |
|
|
@KEV From a quick glance I see a wait(5), wait(2), and a wait(10) in the outer loop. |
|
KEVEKEV77Join Date: 2009-03-12 Post Count: 6961 |
local MinimumPlayers = 2
while true do
--wait for enough players before we start the game
while game.Players.Numplayers < MinimumPlayers do
wait()
end
hm get some glasses |
|
|
while true do -- FIRST DO HERE
--wait for enough players before we start the game
while game.Players.Numplayers < MinimumPlayers do -- SECOND DO HERE
wait()
end -- ONLY ONE END
You are the one who needs glasses. |
|
|
while true do
--wait for enough players before we start the game
while game.Players.Numplayers < MinimumPlayers do
wait()
are you saying I should change it to something more like
while true do
--wait for enough players before we start the game
while game.Players.Numplayers < MinimumPlayers do
wait(1)
end
|
|
|
while true do
--wait for enough players before we start the game
while game.Players.Numplayers < MinimumPlayers do
wait(1)
end
end
So I need 2 ends is the problem? |
|
|
You have the end for the first while loop at the very end of the script by the looks of it. Is there any output? |
|
|
Yes, The output says "Numplayers is not a valid member of Players"
I just saw it say that |
|
|
Well that helps.
NumPlayers
Capital P |
|
EzurasJoin Date: 2012-11-07 Post Count: 8768 |
replace while game.Players.numplayers with game.Players.NumPlayers |
|
|
I did it but for some reason, even with 2 or 3 players it still doesn't teleport them to the track like I have it set up to do. But still, no error message is displayed in output. |
|
|
Try sprinkling in some print statements? |
|
|
I did
while true do
--wait for enough players before we start the game
print ("I am going to wait until there are more players")
while game.Players.NumPlayers < MinimumPlayers do
wait()
end
end
And it did print. But it still doesn't continue the code after there are 2 players. If it detected 2 players it is supposed to allow the code below to begin which starts the race |
|
|
Did you place that second end right after the previous one? If so, delete that. You could also put a print after the inner while loop. |
|
|
When I tested it with 2 players, before the players loaded it printed what I told it to.
Then, when the 2 players loaded it printed again. But they never did get teleported. |
|
|
When I deleted the second end the whole thing bugged out |
|
|
Can you be more specific? |
|
|
while true do
--wait for enough players before we start the game
print ("I am going to wait until there are more players")
while game.Players.NumPlayers < MinimumPlayers do
wait()
end
end
you said to delete one of the ends. I did, and it caused the whole code to mess up because there are 2 whiles in the code so 2 ends are needed for it to work. |
|
|
I also tried this and it failed to work
while true do
--wait for enough players before we start the game
print ("I am going to wait until there are more players")
while game.Players.NumPlayers < MinimumPlayers do
wait()
while game.Players.NumPlayers > MinimumPlayers do
print("There are enough players now")
end
end
end |
|
|
Yes, you should have two while loops and two ends. The first end (for the inner while loop) should be where it is now. The second end (for the outer while loop) needs to go at the end of the script (after the teleporting, etc.) |
|