Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago

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

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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")
0
Well, now I just look stupid! Thank you very much for your help. masterfire113 0 — 9y
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. masterfire113 0 — 9y
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! Perci1 4988 — 9y
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. BlueTaslem 18071 — 9y
Ad

Answer this question