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

KeyDown event works sometimes but sometimes it doesn't? No error yet.

Asked by 9 years ago

When I use a KeyDown event and I test it, it works sometimes but sometimes it doesn't and I'll have to spam the key and it might work.Also, I'm using a localsript.Here's an example of a code:

wait(3)
Player = game.Players.LocalPlayer
Char = Player.Character
debounce = false
duh = 0
mouse = Player:GetMouse()
spe = Char.Humanoid.WalkSpeed

function OnDown(key)
    if debounce == true then return end
    debounce = true
    local Key = key:lower()
    if Key == "q" then
        x = Instance.new("Part")
        local SlashSound = Instance.new("Sound")
SlashSound.SoundId = "http://www.roblox.com/asset/?id=236382703"
SlashSound.Parent = x
SlashSound.Volume = .7
SlashSound:play()
        x.Shape = "Block"
        x.CanCollide = false
        x.Anchored = true
        x.TopSurface = "Smooth"
        x.BottomSurface = "Smooth"
        x.Parent = Workspace
        x.Size = Vector3.new(100, 9, 100)
        x.BrickColor = BrickColor.new("Bright violet")
        x.Name = "Poison"
        m = Instance.new("SpecialMesh", x)
        m.MeshType = "FileMesh"
        m.MeshId = "http://www.roblox.com/asset/?id=1095708"
        m.Scale = Vector3.new(100, 100, 150)
        x.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 0, 0)
        x.Touched:connect(function(Part) 
    if Part.Parent:FindFirstChild("Humanoid") ~= nil and  Part.Parent:FindFirstChild("Humanoid") ~= Player.Character.Humanoid  and Part.Parent.Name ~= "Wall"  then
        if duh == 0 then
            duh = 1
            Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health-25
            wait(0.01)
            duh = 0
        end
    end
end)
        wait(50)
        debounce = false
    end
end

mouse.KeyDown:connect(OnDown)

1 answer

Log in to vote
2
Answered by 9 years ago

Your simple error here is the debounce.Because you declare your debounce as false and you write if debounce == true then return end but it will never happened.For know more about debounce you can look at roblox wiki here

wait(3)
Player = game.Players.LocalPlayer
Char = Player.Character
debounce = true
duh = 0
mouse = Player:GetMouse()
spe = Char.Humanoid.WalkSpeed

function OnDown(key)
    if not debounce then return end
    debounce = false
    local Key = key:lower()
    if Key == "q" then
        x = Instance.new("Part")
        local SlashSound = Instance.new("Sound")
SlashSound.SoundId = "http://www.roblox.com/asset/?id=236382703"
SlashSound.Parent = x
SlashSound.Volume = .7
SlashSound:play()
        x.Shape = "Block"
        x.CanCollide = false
        x.Anchored = true
        x.TopSurface = "Smooth"
        x.BottomSurface = "Smooth"
        x.Parent = Workspace
        x.Size = Vector3.new(100, 9, 100)
        x.BrickColor = BrickColor.new("Bright violet")
        x.Name = "Poison"
        m = Instance.new("SpecialMesh", x)
        m.MeshType = "FileMesh"
        m.MeshId = "http://www.roblox.com/asset/?id=1095708"
        m.Scale = Vector3.new(100, 100, 150)
        x.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 0, 0)
        x.Touched:connect(function(Part) 
    if Part.Parent:FindFirstChild("Humanoid") ~= nil and  Part.Parent:FindFirstChild("Humanoid") ~= Player.Character.Humanoid  and Part.Parent.Name ~= "Wall"  then
        if duh == 0 then
            duh = 1
            Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health-25
            wait(0.01)
            duh = 0
        end
    end
end)
        wait(50)
        debounce = true
    end
end

mouse.KeyDown:connect(OnDown)

2
Thanks. brokenrares 48 — 9y
0
You're welcome ! XToonLinkX123 580 — 9y
Ad

Answer this question