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

What to use for dealing damage from players in a certain range?

So I have a grenade that is still a work in progress, and unlike all the grenade models I could find, it uses a custom explosion particle instead of the standard roblox explosion, but here's the problem; the other grenades use the standard roblox explosion to deal damage, and I don't know what to use to make the grenade deal damage with a particle.

3 comments

0

You could use Region3 when the grenade hits the ground and when it does see if anything is within the region

0

how exactly

0

See the article on Region3. Not too difficult for you to understand. Gets easier if you test with it first too. After using the region, check the distances between the parts inside the region and the origin of the blast.

1 answer

To do this, you have several options. Some of them are using Region3 and then get parts in that region or using magnitude. I will use magnitude right now and then you can look into Region3 later.

local Players = game:GetService("Players");
local totalDamage = 100;
local damageReduce = 1 -- How much you want to reduce the total damage from the player (based on how far the player is from the explosion)

function getPlayersInRange(position, range)
	local players = Players:GetPlayers();
	local foundPlayers = {};
	
	for num, player in pairs(players) do
		local character = player.Character;

		if character then
			local torso = character:FindFirstChild("Torso") or character:FindFirstChild("LowerTorso");
			if torso then
				local distance = (torso.Position - position).magnitude
				if distance <= range then
					table.insert(foundPlayers, { player=player, distance=distance})
				end
			end
		end
	end

	return foundPlayers;
end

function damagePlayers (position)
	local results = getPlayersInRange(position, 50) -- Getting players within 50 studs
	print(#results)
	
	for _, result in pairs(results) do
		local player, distance = result.player, result.distance;
		local playerDamage = (totalDamage - (distance * damageReduce))
		
		local humanoid = player.Character:FindFirstChildOfClass("Humanoid");
		humanoid:TakeDamage(playerDamage);	
		
		print("Dealt", playerDamage, "damage to", player.Name)
	end
end

damagePlayers(workspace.Baseplate.Position)

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