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

I seem to have stumbled upon a difficult to fix problem with my code. How would I fix it?

Asked by 8 years ago
Edited 8 years ago

Before I put in the code, I would like to say what I've done and what the problems are. First of all, it seems as if the debouncer I used isn't working at all, causing some functions to repeat themselves. Second of all, it outputs an error which I'm currently unsure of how to deal with. I've also tried using print statements to find out the issue, but it only tells me that something is activating twice for some unknown reason. Current Output Output: Players being indexed into an array. Player1 Player2 Number Of Player1 Array objects: 2 Random Number: 1 Sniper chosen: Player1 Numbe Of Player1 Array objects: 1 Random Number: 1 Sniper chosen: Player2 true 17:53:26.211 - ServerScriptService.Script:7: bad argument #2 to 'random' (interval is empty)

Current Code:

local Players1 = {}
local Sniper = ""

local IntermissionGUI = game.ReplicatedStorage:WaitForChild("InfoValue")

local function RandomSniper()
    local Random = math.random(1,#Players1)

    Sniper = Players1[Random] 
    print("# Of Player1 Array objects: " .. #Players1)
    print("Random Number: "..tostring(Random))
    print("Sniper chosen: " .. tostring(Sniper))

    table.remove(Players1, Random)

    return true
end

local function CountDown()
  local Sound = Instance.new("Sound",game.Workspace)

        Sound.SoundId = "http://www.roblox.com/asset/?id=134641113"

    for i = 10,0,-1 do
        IntermissionGUI.Value = "Intermissio n: " .. tostring(i)

    Sound:Play()
        wait(1.5)
    Sound:Stop()
    end
  Sound:Remove()
end

local function PlayerToNumber()
    local Players = game.Players:GetPlayers()

  print("Players being indexed into an array.")

    for i, v in pairs(Players) do
        table.insert(Players1,i,v.Name)

        print(Players1[i])
    end

    return true
end

local function SniperToPosition()
    print(RandomSniper())
  local Sniper1 = game.Players:FindFirstChild(Sniper)
    if RandomSniper() == true then


        print("Sniper's Name during the teleportation: "..Sniper1.Name)
    if Sniper1.Character.Torso ~= nil then
        Sniper1.Character.Torso.CFrame = game.Workspace.SpawningPart.CFrame
        print("Sniper Teleport Successful!")
        local Gun = game.ServerStorage.Sniper:Clone()

        Gun.Parent = Sniper1.Backpack
      end
    end
end

local function PlayersToPosition()
    for _, v in pairs(Players1) do
        if v ~= Sniper then
            game.Players:FindFirstChild(v).Character.Torso.CFrame = game.Workspace.PlayerSpawningPart.CFrame + Vector3.new(0,2,math.random(-20,20))
                print("Players being Teleported:" .. v)
            wait(3)
        end
    end
end 

local function RestartTheGame(Player)
  local humanoid = Player.Parent:FindFirstChild("Humanoid")

  if humanoid then
    if game.Players:GetPlayerFromCharacter(Player.Parent) then
    if game.Players:GetPlayerFromCharacter(Player.Parent).TeamColor == "Really Blue" then
        if humanoid.Health > 0 then 
        if game.ReplicatedStorage.GameInProgress.Value then
                    for _, v in pairs(game.Players:GetChildren()) do
            local character = v.Character

            if character then
                            local torso = character.Torso

              if torso then
                torso.CFrame = game.Workspace.SpawningPart.CFrame + Vector3.new(math.random(-8, 8), 2, math.random(-8,8))
              end
            end end
                    end
                end
                    if game.ReplicatedStorage.NumberOfPlayers.Value > 1 then
                        game.ReplicatedStorage.GameInProgress.Value = true

                        CountDown()
                        PlayerToNumber()
                        RandomSniper()
                        SniperToPosition()
                        PlayersToPosition()
            end     
                end
      end
    end
end

game.Players.PlayerRemoving:Connect(function(LeavingPlayer)
    local PlayerCount = game.ReplicatedStorage.NumberOfPlayers.Value

    PlayerCount = PlayerCount - 1
    for i,v in pairs(Players1) do
        if v == LeavingPlayer.Name then
            table.remove(Players1, i)
            if #Players1 < 2 then
                game.ReplicatedStorage.GameInProgress.Value = false
            end
            end
    end
end)


game.Workspace.WinBlock.Touched:connect(RestartTheGame)

game.Players.PlayerAdded:connect(function(NewPlayer)

    local PlayerCount = game.ReplicatedStorage.NumberOfPlayers.Value 

    PlayerCount = PlayerCount + 1

    if game.Players.NumPlayers > 1 and game.ReplicatedStorage.GameInProgress.Value == false then
     print("Game In Progress: " ..tostring(game.ReplicatedStorage.GameInProgress.Value))
        game.ReplicatedStorage.GameInProgress.Value = true
     print("Game In Progress: " ..tostring(game.ReplicatedStorage.GameInProgress.Value))
        CountDown()
        PlayerToNumber()
        RandomSniper()
        SniperToPosition()
        PlayersToPosition()
    end

    NewPlayer.CharacterAdded:connect(function(Character)
        Character:WaitForChild("Humanoid").Died:connect(function()
            if game.ReplicatedStorage.GameInProgress.Value == true then
                if #Players1 > 0 then
                for i,v in pairs(Players1) do
                    if v == NewPlayer.Name then
                        table.remove(Players1,i)
                        if #Players1 < 0 then
                            game.ReplicatedStorage.GameInProgress.Value = false
                            RestartTheGame()
                            end
                        end
                    end
                    end
                else
                    game.ReplicatedStorage.GameInProgress.Value = false
                    RestartTheGame()
                end
                end)
        end)
    end)

0
Also, before anyone complains about the lack of indents in the output description, I was unable to add those, mainly due to the fact that when I ask a question, most of the formatting gets removed. animorphs30 300 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

Good debugging attempt -- it is good to use print statements! However, when you use them, be sure to not call functions that make changes to your variables multiple times accidentally. ex, on line 51, you call RandomSniper() twice (once in a debugging print statement and once legitimately). You should instead do this:

local function SniperToPosition()
    local debug = RandomSniper() --I like to name things like this 'debug' so I know it's not meant to stay there, but call it whatever you like
    print(debug)
    local Sniper1 = game.Players:FindFirstChild(Sniper)
    if debug then --note that "==true" is probably redundant
            print("Sniper's Name during the teleportation: "..Sniper1.Name)
--etc

I say "==true" is probably redundant because in lua, when you say if obj then, the if statement will evaluate as true so long as obj is not false or nil. Try it in the command line: if 5 then print("if is true") end. You can replace '5' with anything you like, including Parts, Vector3s, etc - just not false/nil.

The current bug you are showing is because of this extra call to RandomSniper(). The error means that you are trying to do math.random(1, 0), and it is complaining because there is no valid number it can give you.

In PlayersToPosition you have a consistency "problem": your code there suggests that the Sniper is in Players1, whereas you remove the sniper from Players1 in your RandomSniper function. This isn't necessarily a bug, but I'd recommend fixing it regardless.

Also, in PlayerToNumber, you assume that you need to do something special to the Players object, but the reality is that it is already an array. Further, if there are any left-over players in Players1, you'll have problems (unless you only ever run that function when Players1 is empty). Either way, you can replace that function with this:

local function PlayerToNumber()
    Players1 = game.Players:GetPlayers()
end
0
O_O I honestly just want to say 1 thing, THANKS!!!! I've messaged SO MANY PEOPLE about the issue, including Link... I honestly can't thank you enough, this has been a major issue for me for SOO long!!!! animorphs30 300 — 8y
0
No problem chess123mate 5873 — 8y
Ad

Answer this question