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

Would this bullet script work?

I was just wanting to know if this script would work or not.

ball = script.Parent
local invelocity = ball.Velocity
wait()

function onTouched(hit)
		local human = hit.Parent:findFirstChild("Humanoid")
	if (human ~= nil) then
		human.Health = human.Health - 25  --How much damage the brick does when touched. (100=death when health is normal)
	end
end
script.Parent.Touched:connect(onTouched)

connection = script.Parent.Touched:connect(onTouched)



wait(0.5)

ball.Parent = nil

1 answer

That script would work, although you should probably remove the bullet after it hits someone instead of after 0.5 seconds. That way it won't if it hasn't hit anyone yet. You can also add a timeout so if it never hits anyone it gets removed too, but 0.5 seconds seems too short.

I'm not sure why you made the connection a variable. The only use of that would be if you wanted to disconnect the event later in the script.

Also you should use :Destroy() instead of parenting things to nil to get rid of them.

This would be a more efficient script:


ball = script.Parent
local invelocity = ball.Velocity
wait()
Game:GetService("Debris"):AddItem(ball, 10) -- you could just add wait(10) ball:Destroy() at the end of the script but I think this is fancier :3

function onTouched(hit)
        local human = hit.Parent:findFirstChild("Humanoid")
    if (human ~= nil) then
        human.Health = human.Health - 25  --How much damage the brick does when touched. (100=death when health is normal)
	ball:Destroy()
    end
end
script.Parent.Touched:connect(onTouched)

connection = script.Parent.Touched:connect(onTouched)


3 comments

0

Alright. Thanks. Can you help on that?

0

Also, I have another script in my Laser gun that removes the ball after a couple seconds.

0

In that case you should get rid of one of the methods to remove the ball. Either get rid of the part that removed it in the lazer or remove the Debris part in this script.

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