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

Need help with attack script?

Asked by 9 years ago
Edited 6 years ago

Well, I'm making a game and I'm currently working on the basic attacks, but I have a bit of a problem

wait(2)


function tagHumanoid(humanoid, player, name)
    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = player
    creator_tag.Name = name
    creator_tag.Parent = humanoid
end

function untagHumanoid(humanoid,name)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild(name)
        if tag ~= nil then
            tag:Destroy()
        end
    end
end

local function onKeyDown( key )
    if string.lower(key) == "x" then
    print("R")
    print("E")
    print("S")
    print("E")
    print("T")
    wait(1)
    firing = false
    firstances = 0

end
end



canfire = true
hitsound = script.HitSound
print("Setting variables")
local bin = script.Parent
local user
local player = game.Players.LocalPlayer
local torso = player.Character.Torso
local character = player.Character
local mouse = player:GetMouse()
local rays = character.Rays

firing = 0
firstances = 0

mouse.KeyDown:connect(onKeyDown)


function redfir()
    if firstances > 0 then firstances = firstances - 1 
    end
    end


function castray(object)
        local start = object.Position
    local lookAt = Vector3.new((object.CFrame.lookVector.X*4),0,(object.CFrame.lookVector.Z*4))
    local ray = Ray.new(start, (lookAt))
    hit, position = workspace:FindPartOnRay(ray,character)
    end
    print("Set them, setting functions")


function attack() -- problem is here, two "Instances" of this function start up on the Down value being changed, which causes two basic attacks to fire each time it loops, this is the problem
    wait()


    firstances = firstances + 1
    delay(0.15, function() firstances = firstances - 1 end)
    if firing == false and firstances < 2 then
    firing = true





    wait()
    while bin.Down.Value == true and firstances <= 1 and firing == true do

        print("Attacking!")

        firstances = firstances + 1 print("Increasing")
        delay(0.5, function() firstances = firstances - 1 end)

    for i =1,3 do
    castray(rays[i])
    if hit then break end


        --draw the debug rays for visualization
        local distance = (position - torso.CFrame.p).magnitude
        local rayPart = Instance.new("Part", user)
        rayPart.Name          = "RayPart"
        rayPart.BrickColor    = BrickColor.new("Bright red")
        rayPart.Transparency  = 0.5
        rayPart.Anchored      = true
        rayPart.CanCollide    = false
        rayPart.TopSurface    = Enum.SurfaceType.Smooth
        rayPart.BottomSurface = Enum.SurfaceType.Smooth
        rayPart.formFactor    = Enum.FormFactor.Custom
        rayPart.Size          = Vector3.new(0.2, 0.2, distance)
        rayPart.CFrame        = CFrame.new(position, torso.CFrame.p) * CFrame.new(0, 0, -distance/2)
        rayPart.Parent = workspace

        --add it to debris so it disappears after 0.1 seconds
        game.Debris:AddItem(rayPart, 0.05)
           end
    torso["Right Shoulder"].MaxVelocity = 10000
    torso["Right Shoulder"].DesiredAngle = 3.2

    hitsound:Play()
    --if firstances > 0 then delay(1, function() firstances = firstances - 1 end)

    if hit then

    print(hit.Name)
    local human = hit.Parent:FindFirstChild("Humanoid")
    if human then
        tagHumanoid(human, player,"Killer")
        human:TakeDamage(150)
        print("Nothin' hit")
    end
    wait(1)
    torso["Right Shoulder"].MaxVelocity = 0.1
    torso["Right Shoulder"].DesiredAngle = 0
    untagHumanoid(human,"Killer")










    end

end 


    end

    firing = false
    end






bin.Down.Changed:connect(attack)
print("Done")



while true do
    wait(0.1)
    print (firstances, firing)
end


That's the code, it goes along with a few other things, including another local script which handles the mouse and changes the "Down" value to achieve this. It creates two loops of the attack loop , which is my problem.

Answer this question