I’m trying to develop a landmine in my game, and I want it to make it so that if no player ever steps on the user’s (the user is the player who placed down the landmine) the player can hit the E button to detonate the most recent landmine they’ve put down.
Although I tried doing this, I’m currently facing an issue where for some reason the bombClone (The cloned handle that represents the landmine) is returning back nil. So every time I try to detonate the most recent bomb, I always get an error saying that the explosion particles are index nil, and this is because bombClone even though its an instance, when it gets returned it returns as nil to the local script that has the table of landmines.
client.lua
local MOUSE_ICON = "rbxasset://textures/GunCursor.png" -- Changes the mouse to the GunCursor Icon.
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png" -- Changes the mouse icon to the Gun Wait Curso Icon.
local Player = game.Players.LocalPlayer
local Tool = script.Parent
local cooldown = false
local UserInputService = game:GetService("UserInputService")
local RemoteEvent = script.Parent:WaitForChild("ActivateBomb")
local landmineTable = {} -- The table for landmines.
Tool.Equipped:Connect(function()
UserInputService.MouseIcon = MOUSE_ICON
end)
Tool.Unequipped:Connect(function()
UserInputService.MouseIcon = "rbxassetid://1"
end)
Tool.Activated:Connect(function()
if not cooldown then
cooldown = true
local landmine = RemoteEvent:FireServer()
print(landmine)
print(landmine.Parent)
table.insert(landmineTable, landmine)
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
function explosionParticles(bomb)
print(bomb)
print(bomb.Parent)
local explosionClone = bomb.Explosion:Clone()
explosionClone.Parent = bomb
explosionClone.Enabled = true
return explosionClone
end
function explode(bomb, user)
local explosionParticle = explosionParticles(bomb)
explosionSound:Play()
bomb.Transparency = 1
local region3 = Region3.new(
bomb.Position - Vector3.new(blastRegion3L, 1, blastRegion3W),
bomb.Position + Vector3.new(blastRegion3L, blastRegion3H, blastRegion3W) -- Makes a square that is 5x5 long and wide, and 10 studs high (hence the 1 to 10)
) -- Creates a region3 that destroys any parts in the explosions radius.
local region3Visual = Instance.new("Part") -- The visualized part for the region3.
region3Visual.Name = "blastRadiousVisual"
region3Visual.CanCollide = false
region3Visual.Size = Vector3.new(blastRegion3L, blastRegion3H, blastRegion3W)
region3Visual.CFrame = region3.CFrame
region3Visual.Anchored = true
region3Visual.Transparency = .5
region3Visual.Parent = workspace.Terrain
region3Visual.BrickColor = BrickColor.new("Persimmon")
local explosion = Instance.new("Explosion", bomb) -- The explosion instance
explosion.Position = bomb.Position
explosion.BlastRadius = 0
explosion.DestroyJointRadiusPercent = 0
explosion.BlastPressure = 1000000
explosion.BlastRadius = blastRegion3L
local overlapParams = OverlapParams.new()
overlapParams.FilterDescendantsInstances = {tool, tool.Handle, region3Visual}
local parts = workspace:GetPartBoundsInBox(region3.CFrame, region3.Size, overlapParams)
explosion.Hit:Connect(function(Part)
if Part.Name == "HumanoidRootPart" then
local character = Part.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player and player.Team ~= user.Team then
character:FindFirstChildOfClass("Humanoid"):TakeDamage(baseDamage)
if character:FindFirstChildOfClass("Humanoid").Health == 0 then
user.leaderstats.Kills.Value += 1
end
end
end
end)
for i, v in pairs(parts) do
if v.Name == "Brick" then
v.Anchored = false
v.CanTouch = false
v.BrickColor = user.TeamColor
for i, constraint in pairs(v:GetChildren()) do
if constraint:IsA("Snap") or constraint:IsA("Weld") or constraint:IsA("WeldConstraint") then
constraint:Destroy()
end
end
task.delay(0, game.Destroy, v)
end
if v.Name == "GameBrick" then v.Name = "TaggedGameBrick" -- TaggedGameBricks are GameBricks in the game that have been effected by the bomb, while regular Bricks are just bricks you can destroy by default.
v.Anchored = false
v.CanTouch = false
user.leaderstats.Bricks.Value += 1 -- The total bricks the player has broken throughout the round.
user["T. Bricks"].Value += 1 -- The total bricks that the player has destroyed throughout there playtime
user.leaderstats.Studs.Value += .1 -- Increases the players currency (Studs) by 0.1.
v.BrickColor = user.TeamColor -- Changes the taggedBricks color to the local players team color to show that they have broken the part.
task.delay(0, game.Destroy, v) -- later deletes the part.
end
end
task.delay(0, game.Destroy, bomb)
task.delay(3, game.Destroy, region3Visual)
task.wait(2)
explosionParticle.Enabled = false -- Disables the explosion particles.
end
local Connection
DetonateLandmineRemote.OnServerEvent:Connect(function(player, landmine)
print(landmine)
print(landmine.Parent)
explode(landmine, player)
end)
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)
return bombClone
end)