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

How to correctly assign the same name to a value as a player?

Asked by 6 years ago

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

2 answers

Log in to vote
0
Answered by
PlaasBoer 275 Moderation Voter
6 years ago
Edited 6 years ago

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.

0
Thank you, this worked. Ramshackles 14 — 6y
0
Cool let me if you have any questions. PlaasBoer 275 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
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
0
? DeceptiveCaster 3761 — 6y
0
Creates a "Live" NumberValue for each of the players in the game Le_Teapots 913 — 6y
0
look at red's comment, which is one i deleted. DeceptiveCaster 3761 — 6y
0
you didn't when I posted it lol User#20388 0 — 6y
0
It doesn't print "Enough players present" when a player has joined, I tried adding a wait event but that didn't seem to work. Ramshackles 14 — 6y

Answer this question