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.
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")