Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix this error?

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

✓ Accepted answer

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

1 comment

0

Thanks! and I changed serach to search because I think you spell wrong.

This discussion is preserved as it was. New answers, comments, and votes are closed.