I am making a game in which you fight off waves of enemies, and you are given a certain amount of lives. I felt the best way to do this was to assign a NumberValue to a folder with the value being the amount of lives each player has. However, I receive an error when trying to assign the name of the value. Any help?
Here is my code:
local player = game.Players:GetPlayers() while true do wait() if game.Players.NumPlayers <= 0 then print "No players present." elseif game.Players.NumPlayers >= 1 then print "Enough players present." wait(1) print "Game Starting..." local Live = Instance.new("NumberValue") Live.Name = player.Name Live.Parent = game.ServerStorage.Lives Live.Value = 7 end end
Okay you should also post the error message but I can see what is wrong. And not entirely sure what you want but here is some code. And there is couple of things i see wrong read my explanation I added better code to my understading.
Code is tested in studio.
local player = game.Players:GetPlayers() game.Players.PlayerAdded:Connect(function(player) local Live = Instance.new("NumberValue") Live.Name = player.Name Live.Parent = game.ServerStorage.Lives Live.Value = 7 end) while true do wait(0.1) local player = game.Players:GetPlayers() for i = 1,#player do if #player <= 0 then print "No players present." elseif #player >= 1 then print "Enough players present." print "Game Starting..." --Here you can add the code for the next game maybe the time as wait() wait(60) end end end
Note I think there is a better approach to the while loop
So the game.Players.PlayerAdded is event that fire everytime a player connects and the at that point it adds the live .And make sure you have file named Lives inside game.ServerStorage.
Then in the while loop.So game.Players:GetPlayers() return table with all players inside learn about tables if you don't know it now i added a for loop and the # sign before player give the the amount in table as number so example if you have table local tab = {"bobby","John","Sally"} and you do this print(#tab) it will print 3 since there are 3 stuff in the table now inside the for loop the wait is for the match time before the game ends after the while loops starts over. Reply if you have more questions.
local players = game.Players:GetChildren() while true do wait() if #players <= 0 then print("No players present.") elseif #players >= 1 then print("Enough players present.") wait(1) print("Game Starting...") for i,plr in pairs(players) do local Live = Instance.new("NumberValue") Live.Parent = game.ServerStorage.Lives Live.Name = plr.Name Live.Value = 7 end end end