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

How to fix this error?

Asked by 9 years ago

I am trying fix this code but I get error severals time.

Here error:

14:29:55.826 - Workspace.Script:48: attempt to index a nil value 14:29:55.827 - Stack Begin 14:29:55.828 - Script 'Workspace.Script', Line 48 14:29:55.829 - Stack End

My script:

h = Instance.new("Hint", workspace)
 while true do
timer = 11
repeat
    timer = timer - 1
    h.Text = "Intermission("..timer..")"
    wait(1)
until timer == 0

local sword = game.Lighting:FindFirstChild("LinkedSword")
local players = game.Players:GetChildren()
local maps = math.random(1,4)
if maps == 1 then
h.Text = "Normal Sword Fight!"
game.Lighting.Map1:Clone().Parent = game.Workspace
for _,v in pairs (game.Players:GetPlayers()) do
v.Character:MoveTo(Vector3.new(-167, 40.9, 28.5))
end
local Search = game.Players:GetChildren()
for i = 1, #Search do
sword:Clone().Parent = Search[i].Backpack
end
wait(8)
game.Workspace.Map1:remove()
local Search1 = game.Players:GetChildren()
for i = 1, #Search1 do
Search1[i].Backpack:FindFirstChild("LinkedSword"):remove() 
end
end
for _,v in pairs (game.Players:GetPlayers()) do
v.Character:MoveTo(Vector3.new(-242, 158.5, 52))
end

if maps == 2 then
h.Text = "Windforce Battle!"
game.Lighting.Map2:Clone().Parent = game.Workspace
for _,v in pairs (game.Players:GetPlayers()) do
v.Character:MoveTo(Vector3.new(-0.5, 29.9, -87.5))
end
local Search = game.Players:GetChildren()
for i = 1, #Search do
sword:Clone().Parent = Search[i].Backpack
end
wait(8)
game.Workspace.Map2:remove()
local Search1 = game.Players:GetChildren()
for i = 1, #Search1 do
Search1[i].Backpack:FindFirstChild("LinkedSword"):remove() 
end
end
for _,v in pairs (game.Players:GetPlayers()) do
v.Character:MoveTo(Vector3.new(-242, 158.5, 52))
end
if maps == 3 then
h.Text = "Deadly Venom battle!"
game.Lighting.Map3:Clone().Parent = game.Workspace
for _,v in pairs (game.Players:GetPlayers()) do
v.Character:MoveTo(Vector3.new(98, 14.4, 146.5))
end
local Search = game.Players:GetChildren()
for i = 1, #Search do
sword:Clone().Parent = Search[i].Backpack
end
wait(8)
game.Workspace.Map3:remove()
local Search1 = game.Players:GetChildren()
for i = 1, #Search1 do
Search1[i].Backpack:FindFirstChild("LinkedSword"):remove() 
end
end
for _,v in pairs (game.Players:GetPlayers()) do
v.Character:MoveTo(Vector3.new(-242, 158.5, 52))
end
if maps == 4 then
h.Text = "Death Instant Battle!"
game.Lighting.Map4:Clone().Parent = game.Workspace
for _,v in pairs (game.Players:GetPlayers()) do
v.Character:MoveTo(Vector3.new(103, 1.5, -148)) 
end
local Search = game.Players:GetChildren()
for i = 1, #Search do
sword:Clone().Parent = Search[i].Backpack
end
wait(8)
game.Workspace.Map4:remove()
local Search1 = game.Players:GetChildren()
for i = 1, #Search1 do
Search1[i].Backpack:FindFirstChild("LinkedSword"):remove() 
end
end
for _,v in pairs (game.Players:GetPlayers()) do
v.Character:MoveTo(Vector3.new(-242, 158.5, 52))
end




end

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

You are trying to call remove on the object LinkedSword when it potentially may not exist. In the case where it errors, it does not exist, so you are trying to index a method out of nowhere. The same thing can happen with the Backpack and calling FindFirstChild on it if it isn't there.

Make sure they exist first:

local backpack=Search1[i]:FindFirstChild("Backpack")
if backpack then
    local sword=backpack:FindFirstChild("LinkedSword")
    if sword then
        sword.Parent=nil
    end
end
0
Thanks! and I changed serach to search because I think you spell wrong. AutumnBloxxer 10 — 9y
Ad

Answer this question