Grabbing Objects from a Table IF

it gives a new table that contains only the the values you want to pick from
You shouldn’t edit the new table. edit your original table after the calculation is done!

Oh wait, yeah, I got that, nevermind. How do I go about getting the name of the object’s values that was changed?

1 Like

I’m sorry I rushed to post this. I hope you know break and the while loop condition will stop the loop. That should always be a given with almost all loops. Very few loops are infinite in practice. Yes it was broken but I fixed it.

you already have it! it’s called randomKey!
Wait can you give your code snippet and I’ll edit it for you depending on what you want

1 Like

Maybe something like this would work?

local UsedCharacters = {
	["Rig1"] = false,
	["Rig2"] = true,
	["Rig3"] = false,
	["Rig4"] = true,
}

local function getNotUsed()
	local Clone = { }
	
	for Index, Value in pairs(UsedCharacters)do
		if(not Value)then
			table.insert(Clone, Index)
		end
	end
	
	return Clone
end

local NotUsed = getNotUsed()
local randomNotUsed = NotUsed[math.random(1, #NotUsed)]

UsedCharacters[randomNotUsed] = true
1 Like
local UsedCharacters = {
	["Rig1"] = false;
	["Rig2"] = false;
	["Rig3"] = false;
	["Rig4"] = false;
}

local OnLoadCharacters = ServerStorage:WaitForChild("OnLoadCharacters"):GetChildren()

--Random Character Loader
game.Players.PlayerAdded:Connect(function(Player)
	
	local Math = require(game.ServerScriptService.InnerFunctions)

	local filteredTable = Math.filterT(UsedCharacters, function(value, key)
		return value == false
	end)

	local keys = Math.keysT(UsedCharacters)
	local Rig = keys[math.random(1, #keys)]
	UsedCharacters[Rig] = true
	
	if Rig == "Rig1" then
		print("chosen rig1")
	end
	if Rig == "Rig2" then
		print("chosen rig2")
	end
	if Rig == "Rig3" then
		print("chosen rig3")
	end
	if Rig == "Rig4" then
		print("chosen rig4")
	end

This does work actually, I think everything is good.
I’m basically trying to assign a special character to a player, but it does not assign the same character rig if a player already has it in use.

1 Like

Okay, I think I will be able to whip something up. Before I do you are basically trying to make a system where each player on the server has that special character that no one else is able to use.

Yes, and if they log off it basically sets the Rig they were using back to false, opening it up for another player to use when they join.

Just looking for the absolute efficient way of doing so, I appreciate you all for helping.

1 Like

So it works perfectly now I guess?

Alright, thank you for the clarification!

I believe, I’m going to go about the rest of my script a bit and see if something doesn’t work the way it’s intended.

My solution is the right way to do it. otherwise if you’re talking about performance, then of course there is a better way. but it’s just better to use a general solution like the one I gave you. it’s trading just a bit of performance and temporary memory to give you a solution everytime!
Otherwise am trying to improve and fix my math library, that’s why am interested into your problem!

For a four player server, I don’t really think it makes a big deal, as it will rarely run, and only on the event at which a player joins.

1 Like

You could just do this:

local UsedCharacters = {
   {Rig1 = false},
   {Rig2 = false},
   {Rig3 = false},
   {Rig4 = false}
}
local Rig = UsedCharacters[math.random(#UsedCharacters)]

That selects a random object, disregarding it’s boolean value.

You can check the boolean value?

I could, however if it is false and not true, how else would I have it go back and search for one that is true? So it’d just be quicker to completely get around this to begin with

1 Like

So you want to loop through a table and look for one Rig that is set to true?

A lot of these replies are over-complicated for no reason. There are two simple solutions to this.

One way would be to repeat the random function until the requirements are met. This will not lag, depending on the circumstances. If NO value in the table is set to false, this will run forever and error the script. So, beware:

local Rig
repeat Rig=UsedCharacters[math.random(1,#UsedCharacters)] until Rig==false

The other way is to make a new table containing only the false values, then getting a random value from that table.

local NewTable = {}
for i,v in pairs(UsedCharacters) do
    if v==false then NewTable[#NewTable+1]=v end
end
local Rig = NewTable[math.random(1,#NewTable)]

Let me know if you have any questions.

2 Likes