"attempt to get length of a number value" when trying to get random player

I’ve been trying to fix this for HOURS but I simply can’t figure this out.

here’s my script

local rs = game:GetService("ReplicatedStorage")
local re = rs.RemoteEvents
local bd = re.Birthday
local Evt = bd.Play
local Players = game:GetService("Players")
local play = rs.Birthday.Play
local stg = play.Stages
local val = play.Status
local va2 = play.Time
local Sky = stg.Sky:Clone()
local Bro = stg.Bros_Play:Clone()
local Cast = stg.Castle:Clone()
local Vote = play.VoteQ
local Votee = play.Vote
local Yes = play.Yes
local No = play.No

local plr1 = nil
local plr2 = nil

local stagetbl = {Cast,Bro,Sky}
local word1 = {"Pretty","Weirdly","Stupidly","Builderman's","Epically"}
local word2 = {"Awkward","Funky","Fake","Weird","Epic"}
local word3 = {"Night","Day","Party","Person","Killer"}


local AmountOfPlayers = #Players:GetPlayers()

Players.PlayerAdded:Connect(function(Player)
	AmountOfPlayers = AmountOfPlayers + 1
end)

Players.PlayerRemoving:Connect(function(Player)
	AmountOfPlayers = AmountOfPlayers - 1
end)


local function Play()
if AmountOfPlayers > 1 then
		plr1 = math.random(1,#Players:GetChildren())
		plr2 = math.random(1,#Players:GetChildren())

		plr1.Character.HumanoidRootPart.Position.CFrame = Vector3.new(play.Plr1)
		plr2.Character.HumanoidRootPart.Position.CFrame = Vector3.new(play.Plr1)
	print(3)
	local stagepick = math.random(1,#stagetbl)
	print(stagepick)
	local stage = stagetbl[stagepick]
	print(stage)
	stage.Parent = game.Workspace

	local word1pic = math.random(1,#word1)
	local word1p = word1[word1pic]
	print(word1p)
	local word2pic = math.random(1,#word2)
	local word2p = word2[word2pic]
	print(word2p)
	local word3pic = math.random(1,#word3)
	local word3p = word3[word3pic]
	print(word3p)

	local combined = word1p .. " " .. word2p .." " .. word3p
	val.Value = combined
	
	wait(6)
	
	va2.Value = 30
	repeat va2.Value = va2.Value -1
	wait(1)
	until va2.Value < 1
	print("b")
	plr1.Character.HumanoidRootPart.Position.CFrame = Vector3.new(play.Back)
	plr2.Character.HumanoidRootPart.Position.CFrame = Vector3.new(play.Back)
	Vote.Value = 1
	wait(10)
	Vote.Value = 0
	print("a")
	plr1 = nil		
	plr2 = nil
	if Yes.Value > No.Value then
		Votee.Value = "GOOD PLAY!"
		print("good")
		Yes.Value = 0
		No.Value = 0
	elseif Yes.Value < No.Value then	
	Votee.Value = "BAD PLAY!"
		print("bad")
		Yes.Value = 0
		No.Value = 0
	elseif Yes.Value == No.Value then
		Votee.Value = "the play was ok."
		print("eq")
		Yes.Value = 0
		No.Value = 0
	end			
end
end



Evt.OnServerEvent:Connect(Play)

What line is the error on? And how many of those prints does it get to?

there is no line stated for the error, but it seems to be on line 40, it’s certainly related to the math.random

ended up solving it myself. Thank you to anyone who tried to help.

Would you mind sharing what you did to solve it in the post marked as the solution? The forum prefers all of the questions to have public answers so people searching for the same problem can find solutions, even years down the road.

for anyone viewing this post and searching for an answer:

the lines

plr1 = math.random(1,#Players:GetChildren())
plr2 = math.random(1,#Players:GetChildren())

will only return a number because math.random works with the minimum (1) and the maximum (Player amount)

to select a random player, you need to add :GetPlayers().

plr1 = game.Players:GetPlayers()[math.random(1,#Players:GetChildren())]
plr2 = game.Players:GetPlayers()[math.random(1,#Players:GetChildren())]

this will now get all players and select a random item from that table.

3 Likes

Im aware, I actually was going to do this but I couldn’t open studio at that time and I don’t actually remember what exactly I did so I was going to compare the scripts.

1 Like