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

Probable syntax error: Why does this script break when there seemingly shouldn't be a problem?

PLEASE HELP!

math.randomseed(tick())

playerTbl = {}
plnum = 0
game.Players.PlayerAdded:connect(function(player)
plnum = plnum + 1
playerTbl[plnum] = player
for key, value in pairs(playerTbl) do print(key, value) end
end)

game.Players.PlayerRemoving:connect(function(player)
playerTbl[player] = nil
end)
repeat wait() until plnum > 0
local targetnum = math.random(1, #playerTbl or 1)
	local targetname = playerTbl[targetnum]
	
repeat wait() until game.Workspace:FindFirstChild(targetname)
	script.Parent.Target = game.Workspace:FindFirstChild(targetname):findFirstChild("Head")
	wait(1)
	script.Parent:Fire()

Every time I run this script, it always returns the following: Workspace.ball.RocketPropulsion.Script:16: bad argument #2 to 'random' (interval is empty)

I know this means that when it attempts to create random number using my parameters, the second number is set to 0, but when I attempt to add "repeat wait(1) until #playerTbl > 0" to prevent this, but when I do that it never breaks, and never moves on from that line.

More so, when I make it print the value of #playerTbl a line before I do the repeat line, it prints a value greater than 0 (1), and then returns this: Workspace.ball.RocketPropulsion.Script:22: attempt to index a nil value.

I am aware it probably is a stupid mistake on my part, but I've though this through a dozen times and I cannot for the life of me solve the problem.

1 answer

If it were a syntax error, your script wouldn't run at all.


You're doing this in a weird way. Instead of increasing and decreasing plnum, why not just set it to the actual number of players?

There's also no reason to keep track of the list of players. You can just get it using the :GetPlayers() method of the Players service.


Something like this accomplishes the same as your script but is much, much simpler:

repeat
	wait()
until game.Players.NumPlayers > 0

local players = game.Players:GetPlayers()
local playerNum = math.random(1, #targets)
local player = players[ playerNum ] -- Note that this isn't their NAME
-- It is their player object.

local character = workspace:WaitForChild( player.Name )
-- So you should explicitly use `.Name` when you need their name

script.Parent.Target = character:WaitForChild("Head")

4 comments

0

Well, now I just look stupid! Thank you very much for your help.

0

Oh yeah, and the snippet of code I gave you was only a portion of the actual script, and the list of players was necessary for that portion.

0

Whats the point of the repeat loop at the top? The script won't run until there's at least one player in the server, because otherwise the server wouldn't exist!

0

Perci1: I don't know if that's strictly true. The server could start up before the player is ready enough to actual have a Player object. Besides, without that, this code isn't modular enough to be used in a function or some other repeated loop.

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