Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does this happen?

Asked by
vDraxx 10
8 years ago

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

Log in to vote
0
Answered by
Bulvyte 388 Moderation Voter
8 years ago

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.

0
Thanks, it worked! It's my first time scripting so.. ;P vDraxx 10 — 8y
0
no prob! Bulvyte 388 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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)
Log in to vote
0
Answered by
Turgon 80
8 years ago

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.

0
Do you mean you're killing a Character called "Player" UserOnly20Characters 890 — 8y
0
both the player and the character model are named "Player" Turgon 80 — 8y

Answer this question