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

How to create a constant damage within a sphere?

I'm trying to create a damage effect around a volcano in something I'm working on by having a sphere block and making the player take damage over time while they're inside of it. I've heard of the Region3(?) technique but that seems to only work on cube/rectangle shapes. Is there any way to make a player take damage within this sphere?

edit: the technique ive tried to use is using touched and touchended events, but it only takes damage when the character touches the sphere for the first time or moves while within, then stops. the damage is always all taken at once as well, like 20 per jump original code:

local part = script.Parent

part.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChildOfClass("Humanoid")
	if h then
		h:TakeDamage(5)
	end
	wait(1)
	return
end)

part.TouchEnded:Connect(function()
	return
end)

2 comments

0

this is not a request site

0

That isn't a "request". He provided code and he tried or considered options.

2 answers

I'm not sure but you could insert a script into them and make a loop that compares it's distance to the sphere and say "if distance less than sphere then take damage" Then when they are out of the distance break the loop.

So something like this:



--this is the volcano sphere script.

local function TouchedFunction(touch)
	if touch.Parent:FindFirstChild("Humanoid") then
		local volcanoScript = game.ServerScriptService."DISTANCE SCRIPT HERE"
		local clone = volcanoScript:Clone()
		clone.Parent = touch.Parent
		clone.Disabled = false
	end	
end


"INSERT SPHERE HERE".Touched:Connect(TouchedFunction)

--this is the distance script. PLEASE HAVE THIS SCRIPT IN SERVER STORAGE AND 
 --SELECT ITS PROPERTY AS DISABLED

local humanoidRoot = script.Parent.HumanoidRootPart
while (humanoidRootPart.Position - game.Workspace."INSERT SPHERE HERE".Position).Magnitude <= 100 then

	script.Parent.Humanoid:TakeDamage(20)
	wait(0.2)
end

You can ask for clarification anytime.

Consider using Magnitude. You can run a loop where a script checks the distance between the volcano and all HumanoidRootParts. If the distance is small enough, you can apply damage each time the loop runs, giving you nice consistent damage within a perfect sphere. As RAFA1608 said in the comments, this isn't a request site, so I'll only provide pseudocode for it, but a quick search about Magnitude in ROBLOX LUA should help you.

RootPart = player.HumanoidRootPart
damageDistance = 50 --The distance from the volcano, in studs, you want damage to take place.
x = true
while x do --infinite loop. Set this up how you want
	if (RootPart.Position + VolcanoPart.Position).Magnitude < damageDistance then
		RootPart.Parent.Humanoid.Health = RootPart.Parent.Humanoid.Health - 4
	end
	wait(0.25)
end

My solution might be a little crude, but I strongly advise you look into Magnitude. It may allow you to figure out your own, better solution, specific to your creation's needs. If this answered your question, please mark it as the solution, so that future scripters with your problem (which is actually fairly common) can find this.

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