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

Cannon shell - Kill players in addition to going through bricks?

function onTouched(hit)
	if hit:GetMass() < 70 and not hit.Anchored then
		script.Parent.CanCollide = false
	if hit: -- Insert shot player getting killed script here...
		--and hit.Parent:FindFirstChild("Humanoid") then hit.Humanoid.Health = 0
 	wait(0.8) -- timer so the shell can get to the targeted player in case there is thick armor or multiple layers.
			end
		--hit:BreakJoints()
	end

Trying to add a kill player script to this Armor pricing shell script The if hit: X part I think is my best bet to get the script right, my attempts so far have not killed any humanoids.

What script line should I have in there to make the cannon shell kill humanoids on touch?

1 comment

0

I would think it would be something like, if hit:Get(something to identify hit humanoid entity?) then humanoid.Health = 0 so that the hit humanoid dies.

1 answer

I don't see anything wrong with your proposed script (assuming you uncomment out the commented part of the 'if'), though I always recommend making sure that "hit.Parent" isn't nil before trying to access a property of hit.Parent. I also question your use of the wait command, since your cannon ball might hit multiple parts in quick succession, and each will trigger a new "instance" of the OnTouched function (so you'll have multiple instances of your function running simultaneously, and all will wait for 0.8 seconds)

Anyway, a bug is usually the result of a mismatch between what you believe to be true and what is actually true. To solve this, use "print" statements to figure out what is going on. ex:

function onTouched(hit)
	print("Hit, mass, anchored:", hit, hit:GetMass(), hit.Anchored)
    if hit:GetMass() < 70 and not hit.Anchored then
        script.Parent.CanCollide = false
	print("Humanoid in parent:", hit.Parent:FindFirstChild("Humanoid"))
    if hit.Parent:FindFirstChild("Humanoid") then hit.Humanoid.Health = 0 end
    wait(0.8) -- timer so the shell can get to the targeted player in case there is thick armor or multiple layers.
	print("After wait caused by touching", hit)
            end
        --hit:BreakJoints()
    end

The print statements should help you understand why your script is behaving the way it is and allow you to make corrections. (Of course, you should always delete the print statements once it's working, or else it'll unnecessarily clutter your Output and slow down your game -- the print() command takes time).

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