Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Variables in players?

I have this variable below as part of a minigame script, but is it searching for 'isPlaying' inside a character presumably and if i want to set that variable to 1, where would I have to put it?

for i, player in ipairs(game.Players:GetChildren()) do
	wait(0.1)
	for i,v in pairs(game.Players:GetPlayers()) do
            isplaying = v:FindFirstChild("isPlaying") -- This one...
            if (isplaying) then
                if (isplaying.Value == 1) then
    player.Character.Torso.CFrame = target + Vector3.new(0, 0, i * 5)
			end
		end
	end
end

2 answers

✓ Accepted answer

There is a way to do this. You could add values to a table.

local players = {}

game.Players.PlayerAdded:connect(function(player)
	local data = players[player.Name] or { -- Load the player's data. If it doesn't exist, create it.
		playing = false,
		points = 0,
	}

	data.client = player -- Stores the actual player in the table. This being outside the definition of the table allows it to update who the client is. (It will have the old one, which becomes nil, if they are rejoining. We need the new one.)
	-- do stuff to data here

	players[player.Name] = data
end)

function getPlayingPlayers() -- Just a simple function to iterate through the players and return the playing player objects.
	local playing = {}

	for name, player in pairs(players) do
		if player.playing and player.client and player.client.Parent ~= nil then
			table.insert(playing, player.client) -- Add the actual player to the table
		end
	end

	return playing
end

for index, player in pairs(getPlayingPlayers()) do
	player.Character.Torso.CFrame = CFrame.new(target + Vector3.new(0, 2.5, index * 5))
end

This also allows it to load data from the server for the player so if you don't use DataStore or DataPersistence, it will save on that server.

I don't understand what you're asking. What is "isPlaying"? Is it a value stored in an instance? A better way would be to use a table like this:

_G.playing = {} --make this line run as soon as the server starts, so put it at the beginning of the script or at the beginning of another script. _G is a global variable in which scripts can communicate, so it's useful in situations like these.
for i,player in pairs(game.Players:GetChildren())do
if _G.playing[player.Name] then --playing[player.Name] would be set to true if the player is playing, and nil if they are not.
player.Character.Torso.CFrame = target + Vector3.new(0,0,i*5)
end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.