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

ServerScriptService.MainScript:77: bad argument #2 to 'random' (interval is empty) ??

That's the whole script

-- Variables
DefaultMessageDelayTime = 4
LetterInterval = 0.05
MessageWaitSigns = {',', '?', '.'}
MessageWaitSignDelay = 1


MessageValue = game.ReplicatedStorage.Message
ImageValue = game.ReplicatedStorage.Message.SpeakerImage
SpeakerNameValue = game.ReplicatedStorage.Message.SpeakerName


-- Setup Player Images

PlayerImages = {}


PlayerService = game:GetService('Players')
function MakePlayerImage(Player)
	local Image = ''
	local Success,Error = pcall(function()
		local ID = PlayerService:GetUserIdFromNameAsync(Player.Name) or ''
		local PlayerImage, IsReady = PlayerService:GetUserThumbnailAsync(ID, Enum.ThumbnailType.AvatarBust, Enum.ThumbnailSize.Size420x420)
		if IsReady == true then
			-- Ready to use image
			Image = tostring(PlayerImage)
		else
			-- Use alternative picture
			Image = ("http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&username=%s"):format(Player.Name)
		end	
	end)
	if Success == true then
		-- Successful
	else
		Image = ("http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&username=%s"):format(Player.Name)
	end
	PlayerImages[Player.Name] = Image
end


game.Players.PlayerAdded:Connect(function(Player)
	MakePlayerImage(Player)
end)
for _, Player in pairs(game.Players:GetChildren()) do
	MakePlayerImage(Player)
end



-- Functions

function BroadcastMessage(Text, MessageDelayTime)
	
	for i = 1,#Text,1 do
		MessageValue.Value = string.sub(Text,1,i)
		for _, L in pairs(MessageWaitSigns) do
			if string.sub(Text,i,i) == L then
				wait(MessageWaitSignDelay)
			end
		end
		wait(LetterInterval)
	end
	if tonumber(MessageDelayTime) ~= nil then
		wait(MessageDelayTime)
	else 
		wait(DefaultMessageDelayTime) 
	end
	MessageValue.Value = 'None'
	return true
end


function SetMessageSpeaker(Name)
	if Name == 'Random' then
		local RandomPlayer = game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())]
		Name = RandomPlayer.Name
	end
	ImageValue.Value = PlayerImages[Name] or '' 
	SpeakerNameValue = Name
end


















------------------------------ MAIN SCRIPT OF STORY ------------------------------------------------


SetMessageSpeaker('Random')

BroadcastMessage('Hello .... My name is Shebel .... I will be your guide!')




2 answers

There's a function for :GetPlayers(), so you can try that. The error I think means your math.random value of #GetChildren() is empty.

local RandomPlayerList = game.Players:GetPlayers()
local RandomPlayer = math.random(1,#RandomPlayerList)
Name = RandomPlayer.Name

2 comments

1

math.random returns a number, not a reference to the player.

0

Thank you so much for helping me :)

A little correction to another Answer. NowUndoThatMistake is correct, math.random returns a number regardless of anything else.

So you need to have the list before such as

local chosen = list[randomNumber] -- this is basically what you're doing .. sooo
local list = game:GetService("Players"):GetChildren() -- this is a list of players in the game

local chosen = list[randomNumber] -- okay list is complete, now how do we get the randomNumber?

local randomNumber = math.random(#list) -- this gets the list and returns how many are within it.

--- sooooo....

local chosen = list[randomNumber]
print(chosen) -- this will show what you want lol

So everything together would be...

local playerList = game:GetService("Players"):GetPlayers()
local randomPlayer = playerList[math.random(#playerList)]
print(randomPlayer.Name)

-- feel free to ask questions!

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