How to damage Player with my Script?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

How to damage Player with my Script?

  1. What is the issue? Include screenshots / videos if possible!

How to damage Player with my Script?

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I haven’t tried any solutions so far. I did look for solutions on the Developer Hub.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block
local ray = {}

local parent = script.Parent
local particleEmitter = parent['ParticleEmitter']
local maxLifetime = parent['MaxLifetimeValue'].Value

do
	local self = ray
	
	self.collisions = function()
		local speed = particleEmitter.Speed
		
		local particleRay = Ray.new(
			parent.CFrame.p,
			((parent.CFrame * CFrame.new(0,10,0).p) - parent.CFrame.p).unit * (2048 + 20)
		)
		
		local part,pos = workspace:FindPartOnRay(particleRay)
		
		if part then
			local distance = (pos - parent.Position).magnitude
			local collisionTime = NumberRange.new(distance / speed.Min)
			local currentLifetime = math.min(collisionTime.Min,maxLifetime)
			
			particleEmitter.Lifetime = NumberRange.new(currentLifetime)
		else
			particleEmitter.Lifetime = NumberRange.new(maxLifetime)
		end
	end
end

spawn(function()
	while true do
		ray.collisions()
		wait()
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I presume this is within a Part running in a Server Script
(If its a local script - you’ll need to call a remote event, and run your damage on the server)

if part then
	local hum = part.Parent:FindFirstChildOfClass("Humanoid") or part.Parent.Parent:FindFirstChildOfClass("Humanoid")
	if (hum) then
		hum:TakeDamage(10) -- change as necesary
--etc

To explain the code a little - I’m looking in both the Raycast’s hit part’s Parent for Legs, Arms, Head, Torsos, etc, but also included parent.Parent.Parent in case we hit an item of clothing, such as a badge, hat, scarf, etc, which are sometimes parented to the player’s head/torso.

Also - you may want to look into the new Raycast solution which is faster and more versatile.
Intro to Raycasting (roblox.com)
WorldRoot:Raycast (roblox.com)

1 Like
local ray = {}
local parent = script.Parent
local particleEmitter = parent['ParticleEmitter']
local maxLifetime = parent['MaxLifetimeValue'].Value
do
	local self = ray
	self.collisions = function()
		local speed = particleEmitter.Speed
		local particleRay = Ray.new(
			parent.CFrame.p,
			((parent.CFrame * CFrame.new(0,10,0).p) - parent.CFrame.p).unit * (2048 + 20)
		)
		local part,pos = workspace:FindPartOnRay(particleRay)
		if part then
			if part.Parent:FindFirstChild("HumanoidRootPart") then
				local humanoid = part.Parent:FindFirstChild("Humanoid")
				humanoid:TakeDamage(-10)
			end
			local distance = (pos - parent.Position).magnitude
			local collisionTime = NumberRange.new(distance / speed.Min)
			local currentLifetime = math.min(collisionTime.Min,maxLifetime)
			particleEmitter.Lifetime = NumberRange.new(currentLifetime)
		else
			particleEmitter.Lifetime = NumberRange.new(maxLifetime)
		end
	end
end

spawn(function()
	while task.wait() do
		ray.collisions()
	end
end)
1 Like