How to detect the killed player by an explosion?

How to detect the killed player by an explosion and how to tag this C4 so you get cash on kill?

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	game:GetService("Debris"):AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end


game.ReplicatedStorage.BombFire.OnServerEvent:Connect(function(player)
	if not game.Workspace:FindFirstChild(player.Name.."Bomb") then
		local newBomb = game.ReplicatedStorage.BombClone:Clone()
		newBomb.Parent = game.Workspace
		newBomb.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-7)
		
		player.Character:FindFirstChild("C4"):Destroy()
		player.Character.Humanoid:UnequipTools()
		
		wait(1)

		newBomb.Anchored = true
	end
end)



game.ReplicatedStorage.BombCallback.OnServerEvent:Connect(function(player)
	if game.Workspace:FindFirstChild(player.Name.."Bomb") then
		local bomb = game.Workspace:FindFirstChild(player.Name.."Bomb")
		
		bomb.Sound:Play()
		
		wait(bomb.Sound.TimeLength)
		
		bomb.Transparency = 1
		bomb.CanCollide = false
		
		local explosion = Instance.new("Explosion")
		explosion.Parent = game.Workspace
		explosion.Position = bomb.Position
		explosion.BlastRadius = 10
		explosion.BlastPressure = 50000
		
		bomb.explosion:Play()
		
		wait(bomb.explosion.TimeLength)
		bomb:Destroy()
	else
		--nothing
	end
end)
2 Likes

Hit is an Explosion event, which fires every time the BasePart has been hit by it.

So, what you could do is name the explosion your player’s name, then connect the function to the event, where you check if it’s the character other than a player who thrown the bomb and then tag them.

2 Likes

To tag:

-- Firstly we need to make the explosion deal damage instead of breaking joints
-- You can just keep the joint-breaking part like before if you want, it doesn't really matter
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 10
explosion.BlastPressure = 50000
explosion.DestroyJointRadiusPercent = 0
explosion.ExplosionType = Enum.ExplosionType.NoCraters
explosion.Position = bomb.Position
explosion.Parent = game.Workspace

-- I'll assume you already have the bomb's owner defined somewhere for the "Player" varible
-- For the TagHumanoid function, the first argument is usually the target's Humanoid, and then the next one is the creator's name
explosion.Hit:Connect(function(Hit)
	if Hit and Hit.Parent and Hit.Parent ~= player.Character then -- Make sure to exclude the character of the bomb's owner
		local Character = Hit.Parent		
		if Character:FindFirstChildOfClass("Humanoid") then
			local Humanoid = Character:FindFirstChildOfClass("Humanoid")
			UntagHumanoid(Humanoid)
			TagHumanoid(Humanoid, player) <-- You need the the bomb's owner varible here
			Humanoid:TakeDamage(YourDamage)
		end
	end
end)

To detect the killer:

Humanoid.Died:Connect(function()
	if Humanoid:FindFirstChild("creator") and Humanoid:FindFirstChild("creator").Value ~= nil then
		local Killer = Humanoid:FindFirstChild("creator").Value
		for i, v in ipairs(game:GetService("Players"):GetPlayers()) do
			if v and v.Name == Killer then
			-- Give the reward here
			end
		end
	end
end)
3 Likes
function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	game:GetService("Debris"):AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end


game.ReplicatedStorage.BombFire.OnServerEvent:Connect(function(player)
	if not game.Workspace:FindFirstChild(player.Name.."Bomb") then
		local newBomb = game.ReplicatedStorage.BombClone:Clone()
		newBomb.Parent = game.Workspace
		newBomb.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-7)
		
		player.Character:FindFirstChild("C4"):Destroy()
		player.Character.Humanoid:UnequipTools()
		
		wait(1)

		newBomb.Anchored = true
	end
end)



game.ReplicatedStorage.BombCallback.OnServerEvent:Connect(function(player)
	if game.Workspace:FindFirstChild(player.Name.."Bomb") then
		local bomb = game.Workspace:FindFirstChild(player.Name.."Bomb")
		
		bomb.Sound:Play()
		
		wait(bomb.Sound.TimeLength)
		
		bomb.Transparency = 1
		bomb.CanCollide = false
		
		local explosion = Instance.new("Explosion")
		explosion.Parent = game.Workspace
		explosion.Position = bomb.Position
		explosion.BlastRadius = 10
		explosion.BlastPressure = 50000
		
		explosion.Hit:Connect(function(Hit)
			if Hit and Hit.Parent and Hit.Parent ~= player.Character then
				local Character = Hit.Parent		
				if Character:FindFirstChildOfClass("Humanoid") then
					local Humanoid = Character:FindFirstChildOfClass("Humanoid")
					UntagHumanoid(Humanoid)
					TagHumanoid(Humanoid, player)
					Humanoid:TakeDamage(200)
				end
			end
		end)
		
		bomb.explosion:Play()
		
		wait(bomb.explosion.TimeLength)
		bomb:Destroy()
	else
		--nothing
	end
end)

I made it like this. I have already made the script to give the Cash and XP!

2 Likes

Perfect
You can mark that as the solution if there’s no other issue

3 Likes

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