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

How to know if a player owns the value already?

I am having problems with a script, which gives away special values to players upon joining (all values are located in a table). Each time the 3rd player joins, the value is repeated (cause its already owned by 2nd player), and this script tries to notice if a chosen value is already owned by someone. Though, I am aware that it checks only one player's value, I don't know how to check ALL player values. Any answers?


local PlayerIndexTable = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

local function PlayerAdded(player)
	local PlayerNum = game.Players:GetChildren()
	if #PlayerNum == 1 then
		local Index = Instance.new("IntValue")
		Index.Parent = player
		Index.Value = 1
	end
	if #PlayerNum > 1 then	
		for i,v in pairs(PlayerNum) do
			if v:FindFirstChild("Value") ~= nil then
				local ChosenValue = next(PlayerIndexTable, i)
				if PlayerNum[i].Value.Vale ~= ChosenValue then
					local Index = Instance.new("IntValue")
					Index.Parent = player
					Index.Value = ChosenValue
				end
			end	
		end		
	end	
end

game.Players.PlayerAdded:Connect(PlayerAdded)

1 comment

0

Maybe another way to go about this would be to add a value to the player when they join, and then remove that value from the table of possible values. That way, everyone is getting a unique value without any checks.

1 answer

I'm bad at explaining but here, When player joins, go throughs the "playerIndex" table and check the index's value for nil. If nil, sets the indexValue to i. When leaving, sets the index's value to nil.

local playerIndex = {}

local function playerAdded(player)
	local index = Instance.new("IntValue", player)
	for i = 1, 10 do
		if playerIndex[i] == nil then
			playerIndex[i] = player
			index.Value = i
			break
		end
	end
end

local function playerRemoving(player)
	for i, v in pairs(playerIndex) do
		if v == player then
			playerIndex[i] = nil
			break
		end
	end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoving)

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