You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I’m trying to make the game give the player a random spawn, 1 for each player.
What is the issue? Include screenshots / videos if possible!
When using the script, the part that puts the player at spawn warns “attempt to call a table value” even though it should choose a random item and return only one.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried using the same script as I found on the devforum as a solution to someone trying to pick a random number, and it seemed to work fine for them.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local function findspawn(folder)
local chosenspawn
repeat
local items = folder:GetChildren()
chosenspawn = items[math.random(#items)]
until chosenspawn.Used.Value == false
chosenspawn.Used.Value = true
return chosenspawn
end
local emptyspawn = findspawn(mineCLN.Spawns)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
This was mean’t for a folder Instance, judging from the error, are you feeding the function a table?
If you want to do that instead then use
local function findspawn(items)
local chosenspawn
repeat
chosenspawn = items[math.random(#items)]
until chosenspawn.Used.Value == false
chosenspawn.Used.Value = true
return chosenspawn
end
local emptyspawn = findspawn(mineCLN.Spawns)
sorry for the late reply, Yes, mineCLN.Spawns is a folder. When I tried your solution, the following error message printed: Workspace.RoundsSystem.Events.SweepEvent.Sweeper:32 (which is the chosenspawn = items[math.random(#items)] line) attempt to get length of a Instance value
EDIT: this did work. the problem was that later I tried adding vector3 to a CFrame.
IMO, there is no reason to do rejection sampling here. Just fetch your items array of spawn pads one time, and shuffle the array. Then you can just assign players one by one to a spawn in the order they are in the shuffle, without having to retry or store Used state at all, or risk an infinite loop condition.
Knuth-Fisher-Yates shuffle will be sufficient to shuffle the array. It’s just a few lines of code that randomly swaps pairs of array elements.
thanks for the advice, but this is not the problem I am trying to solve right now. the “v.Character.Torso.CFrame = emptyspawn.CFrame + Vector3(0,5,0)” line prints an error: attempt to call a table value after using the function.