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

attempt to index a nil value?

Asked by 6 years ago

On line 25, the script gives the error of attempt to index a nil value - Stack Begin.

The script is suppose to loop through so for do loops, and then put the players onto there respective pads.

--//Services//--
local players = game:GetService('Players')
--//Variables//--
local timeInt = 60
local intTime = 60
local pretime = 10

--//Round Objects//--
local status = game.ReplicatedStorage.Status
local spawnPoints = game.Workspace:FindFirstChild('Spawnpoints')
--//Main//--
while wait() do

    for i=intTime,1,-1 do
        wait(1)
        status.Value = 'intermission'
    end

    local currPlayers = {}
    for i,Player in pairs(players:GetPlayers()) do
        table.insert(currPlayers,Player)
    end

    for i,Player in pairs(currPlayers) do
        Player.Character:SetPrimaryPartCFrame(spawnPoints:FindFirstChild('Points' ..i).CFrame + Vector3.new(0,3,0))
    end 

    for i=pretime,1,-1 do
        status.Value = 'prematch'
        wait(1)
    end

    for i=timeInt,1,-1 do
        wait(1)
        status.Value = 'game'
    end
end

THANKS! :D

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

A few problems here, I'll go over the easy ones first

There's no need to loop through players:GetPlayers() and sort it into a table because what you get from that function should be exactly the same as what you get from your for loop.

local currPlayers = players:GetPlayers()

The second minor error is that there is no need to set a variable for the players if it only gets used once (which, from your current code, it does).

for i,Player in pairs (players:GetPlayers()) do
    -- teleport
end

The game breaking problem is that I assume the pads are not being found. First I recommend you split the 'get the pads' part it from the other code, and make it a variable. Then, you make a check to see if it actually exists.

local pad = spawnPoints:FindFirstChild('Points' ..i)
if pad ~= nil then
    -- teleport
else
    warn("Could not teleport " ..Player.Name.. " to pad " ..i.. " because it does not exist") -- example of an error message, warn is like print but it's colored yellow
end

This should make your code 'bug proof' so it won't break the game when this problem happens. Now you can get on to debugging.

Possible causes of this problem:

  • When you named your points, you put a space between 'Points' and the number (Points 1 instead of Points1). When concatenating (adding together) strings ("string1".."string2"), spaces are not added, so you should add one into your FindFirstChild: spawnPoints:FindFirstChild("Points "..i)
  • You may have added a 's' by mistake at the end of 'point' in your FindFirstChild. So you just remove it: spawnPoints:FindFirstChild("Point"..i)
  • Lastly, the spawn point may not actually be there, so you will have to make sure it is before this code will work properly.

Hope I could help!

~TDP

Ad

Answer this question