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

Why does this happen?

I just started learning how to script and I made a part that can kill you when touched. But my problem is that every time I play it on studio, it kills me before I even touch the part.

Code:

debounce = false
brick = game.Workspace.Part

function onTouched()
	if debounce == true then return end
	debounce = true
	game.Workspace.Player.Head:Destroy()
	wait(2)
	debounce = false
end

script.Parent.Touched:connect(onTouched)

3 answers

✓ Accepted answer

Why would u want to put a debounce on kill brick ? if someone touches it in 2 seconds anyone can pass it beng invisible.

Without debounce

function onTouched(hit)
		if hit.Parent:findFirstChild("Humanoid")~=nil then
		hit.Parent.Humanoid.Health=0
		end
  	end

script.Parent.Touched:connect(onTouched)

With debounce

ting = 0
function onTouched(hit)
		if ting == 0 then
		ting = 1
		if hit.Parent:findFirstChild("Humanoid")~=nil then
		hit.Parent.Humanoid.Health=0
		wait(2)
		ting = 0
		end
  	end
end

script.Parent.Touched:connect(onTouched)

try this. If it doesn't work tell meh.

2 comments

0

Thanks, it worked! It's my first time scripting so.. ;P

0

no prob!

No offense but but your "Killing" script is inefficient and since the function can be activated by a practically anything that is touching your Part which is why its killing you before you touch the Part.

Debounce = false
script.Parent.Touched:connect(function(Hit)--Function listen for a touched Event
	local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)--Checks to see if the Part or object that activated the function is controlled by a Player.
if	Player and Debounce ~= true then--If its Controlled by and Player and Debounce is not true then...
	Debounce = true
	Player.Character.Humanoid.Health = nil--Gets the Players Character's Humanoid Health and turn it to nil(Zero)
	Debounce = false
	end
end)

Youre not checking if the part is touching a player, you're just checking if its touching anything at all. Im guessing it's touching the baseplate which is why it kills your character.

Note that this wouldnt work in online mode because youre only killing the player called "Player", if your name is anything different it would break the script because it can't find the object "Player" in the workspace.

2 comments

0

Do you mean you're killing a Character called "Player"

0

both the player and the character model are named "Player"

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