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

how to add damaging to this new instance?

ok fellas, I've met a roadblock. I have this script, and im out of ideas on how to make it so that this darned new instance can cause damage when to players touching it... whether you can help or not, I thank you for reading this...

local tool = script.Parent
local mouse

tool.Equipped:Connect(function(m)
	mouse = m
end)

tool.Activated:Connect(function()
	if mouse then
		local Part = Instance.new("Part",workspace)
		Part.Position = script.parent.Handle.Position
		Part.Material = Enum.Material.Neon
		Part.Size = Vector3.new(0.5,0.5,0.5)
		Part.CanCollide = false
		Part.Anchored = true
--damage thing goes here
	end
end)

sorry for posting a question that's very similar to my previous one but I really cant think of anything since I'm new and I suck at coding.

1 answer

✓ Accepted answer

All you would need to do is use the variable you set for the part and connect the touched event to deal damage. That would make your script look like this:

local tool = script.Parent
local mouse
local debounce = false 
local dmg = --set damage here

tool.Equipped:Connect(function(m)
    mouse = m
end)

tool.Activated:Connect(function()
    if mouse then
        local Part = Instance.new("Part",workspace)
        Part.Position = script.parent.Handle.Position
        Part.Material = Enum.Material.Neon
        Part.Size = Vector3.new(0.5,0.5,0.5)
        Part.CanCollide = false
        Part.Anchored = true
	
	Part.Touched:Connect(function(hit)
		local h = hit.Parent:FindFirstChild("Humanoid")
		if h then
			debounce = true
			h:TakeDamage(dmg)
			wait(1)
		end
		debounce = false
	end)
    end
end)

I did a little more than adding touched. One thing I did was add debounce so that the part could only deal damage once every second. The second thing I did was add a variable named "dmg" so you could easily change how much damage the part deals. Hope this helped!

2 comments

0

also please ask any questions you might have in the comments if you need something explained (or if the code doesn't work).

0

thanks, your a life saver!

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