After returning an instance, it for some reason comes back as nil

I’m trying to make it so that players can detonate the most recent landmine so that if it doesn’t go off they can manually make it go off. Although, the issue is after returning the bombHandle Instance to the local script so that it can make a variable called landmine equal to the Instance, for some reason landmine is nil even though I know that I returned an instance.

Why am I having this issue?

client.lua

Tool.Activated:Connect(function()
	if not cooldown then 
		cooldown = true
		
		local landmine = RemoteEvent:FireServer() -- Makes the landmine equal to the bombClone after it gets returned
		
		print(landmine)
		print(landmine.Parent)
		table.insert(landmineTable, landmine) -- Supposed to insert the landmine into a table, but for some reason landmine is always equal to nil.
			
		UserInputService.MouseIcon = RELOADING_ICON
		
		task.wait(.5)

		UserInputService.MouseIcon = MOUSE_ICON
				
		cooldown = false
	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.E and not gpe then
		Tool:WaitForChild("DetonateLandmine"):FireServer(landmineTable[1])
	end
end)

server.lua

DetonateLandmineRemote.OnServerEvent:Connect(function(player, landmine)
	explode(landmine, player)
end)

local Connection
ActivateBombRemote.OnServerEvent:Connect(function(user) -- This is the function that makes the bomb go off, which connects the ActivateBomb Remote Event to this script after it gets activated by the local script.
	local bombClone = bombHandle:Clone()
	bombClone.Parent = game.Workspace
	bombClone.CanCollide = true
	bombClone.CFrame = bombHandle.CFrame * CFrame.new(0, 2, -4)
	
	task.wait(3)

	Connection = bombClone.Touched:Connect(function(hit)
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			Connection:Disconnect()
			explode(bombClone, user)
		end
	end)
	
	print(bombClone)
	
	return bombClone -- Returns the Instance bombClone
end)

A RemoteEvent is a one-way signal to the server, and cannot return anything.
For this case, you want to use RemoteFunction, because it yields the current thread until the function ends, or a value has been returned.

If you have any trouble, you can take a look at the documentation:

1 Like

Ok so, the landmine is now considered the bomb, but for some reason every time I try to add it to the table It still prints out nil…

  18:04:00.866  Handle  -  Client - Activate:27
  18:04:00.869  nil  -  Client - Activate:30
  18:04:00.870  Handle  -  Client - Activate:31

image

The table just looks like this, so what’s wrong with it?

local landmineTable = {} -- The table for landmines.

There is lag between Client and Server.

Not sure asking the Server to define a variable then instantly printing that variable from a Client script will work.

There is not enough time for it to equal anything but nil.\ or whatever it already was.

So do I just add a wait function?

And also, even though there is lag between the server, how come the landmine variable gets set up but for some reason when I try to check if its in the table, its not?

I have done that.

You can add one just as a test to see if it fixes the problem.

Then maybe figure out a more reliable way to get the result you want.

This is definitely not the issue, as its just whenever I try to find the index value of 1 in the table, it comes out as nil.

Here’s what I wrote in the code as comments to show what is happening.

		table.insert(landmineTable, landmine)
		print(landmineTable[landmine]) -- Checking this alway seems to return as "nil" for some reason..
		print(landmineTable[1]) -- This actually prints out the bomb, so why is it that landmineTable[landmine] returns as nil?
		print(landmineTable) -- printing this actually shows me that the bomb is in the index of one, which makes me want to ask the question again, why isn't this making the bomb print "bomb"?

The issue is how you inserted the item in the array, basically, the key is numeric and equal to #landmineTable(the last element in the table), not the instance itself. So both of the following work:

print(landmine)
print(landmineTable[#landmineTable])

Also landmineTable[1] shows the first element, so it’s not reliable unless the table only has a single element which doesn’t look practical.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.