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:
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?
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: