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

KeyDown script will not activate?

Asked by 9 years ago

Okay, so for some reason, this onKeyDown script won't work. It doesn't say anything in the output and I've already tested it in a live game...

player = game.Players.LocalPlayer
char = player.Character
mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    key = key:lower()
    if key == "z" then
        local hole = Instance.new("Part", char)
        local holemesh = Instance.new("CylinderMesh", hole)
        hole.FormFactor = "Custom"
        hole.Anchored = true
        hole.CanCollide = false
        hole.BrickColor = BrickColor.new("Really black")
        local script = script.Script:Clone()
        script.Parent = hole
        script.Disabled = false
        char.Torso.Anchored = true
        for i = 1, 100 do
            hole.Size = Vector3.new(i, 0.2, i)
            hole.CFrame = char.Torso.CFrame * CFrame.new(0, -2.8, 0)
            wait(0.025)
        end
        game.Debris:AddItem(hole, 2)
        wait(0.025)
        char.Torso.Anchored = false
    end
end)

And if it matters, the damage script.

script.Parent.Touched:connect(function(toucher)
    local humanoid = toucher.Parent:FindFirstChild("Humanoid")
    if humanoid then
        if toucher.Parent.Name ~= script.Parent.Parent.Name then
            toucher.Parent.Torso.Anchored = true
            toucher.Parent.Torso.CanCollide = false
            toucher.Parent["Right Leg"].CanCollide = false
            toucher.Parent["Left Leg"].CanCollide = false
            for i = 0, 0.5, 0.1 do
                toucher.Parent.Torso.CFrame = toucher.Parent.Torso.CFrame * CFrame.new(0, -i, 0)
                wait(0.5)
            end
            humanoid.Health = humanoid.Health - 25
        end
    end
end)
0
Delete line 7 and replace line 6 with: if key:lower()== "z" then UserOnly20Characters 890 — 9y
0
Is this in a localscript in somewhere that replicates to the client(e.g. StarterGui, StarterPack, etc..) Goulstem 8144 — 9y
0
@User Doesn't work. @Goul It's a localscript inside starterpack. InfraredChasm 35 — 9y
0
If anyone could debug this, I would be very grateful and in debt to you. InfraredChasm 35 — 9y
View all comments (2 more)
0
I skim read the script and I doesn't see anything wrong except from the line 6 and 7 which I already pointed out. UserOnly20Characters 890 — 9y
0
@UserOnly16Characters Lines 6 & 7 are valid. Redbullusa 1580 — 9y

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

I recommend you to fix:

  • First Script - Lines 1 & 2, line 10, & lines 14-16

  • Second Script - Lines 5-8


FIRST SCRIPT

  • Lines 1 & 2 - Local scripts are fast, because they run from the client (you). Because they are fast, this script will run before the local player is defined, so player will come up "nil". Because of this, character models may come up "nil" also (error for character model should show up in the output, unless this was tested in solo mode).
repeat
    wait()
until game.Players.LocalPlayer
Player = game.Players.LocalPlayer
Character = Player.Character
if not Character then
    Character = Player.CharacterAdded:wait() -- This is added to wait until the character model is not nil.
end
hole.FormFactor = Enum.FormFactor.Custom
  • Lines 14-16 - I wouldn't name a variable as "script", because "script" by default is the running script itself. I'm not sure if the code will execute if you leave it as is. Rename it if you, too, are unsure.
local DamageScript = script.Script:Clone()
DamageScript.Parent = hole
DamageScript.Disabled  = false

SECOND SCRIPT

  • Lines 7 & 8 - We don't know yet if the entity that touched the hole has arms/legs. A character model can still function without them.
local RightLeg = toucher.Parent:FindFirstChild("Right Leg")
if RightLeg then
    RightLeg.CanCollide = false
end
local LeftLeg = toucher.Parent:FindFirstChild("Left Leg")
if LeftLeg then
    LeftLeg.CanCollide = false
end

repeat
    wait()
until game.Players.LocalPlayer
Player = game.Players.LocalPlayer
Character = Player.Character
if not Character then
    Character = Player.CharacterAdded:wait()
end
Mouse = Player:GetMouse()

DamageScript = script.Script

Mouse.KeyDown:connect(function (key)
    key = key:lower()
    if key == "z" then
        local Torso = Character:FindFirstChild("Torso")
        local hole = Instance.new("Part", Character)
        local holemesh = Instance.new("CylinderMesh", hole)
        hole.FormFactor = Enum.FormFactor.Custom
        hole.Anchored = true
        hole.CanCollide = false
        hole.BrickColor = BrickColor.new("Really Black")
        local DamageScriptClone = DamageScript:Clone()
        DamageScriptClone.Parent = hole
        DamageScriptClone.Disabled = false
        game.Debris:AddItem(hole, 2)
        if Torso then
            Torso.Anchored = true
            for i = 1, 100 do
                hole.Size = Vector3.new(i, .2, i)
                hole.CFrame = Torso.CFrame * CFrame.new(0, -2.8, 0)
                wait(.025)
            end
            wait(.025)
            Torso.Anchored = false
        end
script.Parent.Touched:connect(function (hit)
    local Victim = hit and hit.Parent
    local Humanoid = Victim and Victim:FindFirstChild("Humanoid")
    if Humanoid then
        if Victim.Name ~= script.Parent.Parent.Name then
            local Torso = Victim:FindFirstChild("Torso")
            local RightLeg = Victim:FindFirstChild("Right Leg")
            local LeftLeg = Victim:FindFirstChild("Left Leg")
            if RightLeg then
                RightLeg.CanCollide = false
            end
            if LeftLeg then
                LeftLeg.CanCollide = false
            end
            if Torso then   
                Torso.Anchored = true
                Torso.CanCollide = false
                for i = 0, .5, .1 do
                    Torso.CFrame = Torso.CFrame * CFrame.new(0, -i, 0)
                    wait(.5)
                end
            end
            Humanoid.Health:TakeDamage(25)
        end
    end
end
0
Alright, trying this out right now. InfraredChasm 35 — 9y
0
Worked. Thank youuuuuuu! InfraredChasm 35 — 9y
0
You're welcome. Redbullusa 1580 — 9y
Ad

Answer this question