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

How to keep weapon swinging while Left Mouse Button is Down?

Asked by 4 years ago
Edited 4 years ago

Hi!

I got a Tool that I want to keep swinging while the left mouse button is down. I've tried following the UserInputService on the wiki and its confusing me. How would I incorporate this service into ensuring the Player's tool keeps swinging as long as the left mouse button is down? I reverted my script back to where it works fine with multiple clicks.

Local Script 1


local tool = script.Parent local event = tool:WaitForChild("RemoteEvent") event.OnClientEvent:connect(function(...) local tuple = {...} if tuple[1] == "RunAnimation" then local anim = tool:FindFirstChild(tuple[2]) if anim and humanoid then local loadedanim = humanoid:LoadAnimation(anim) if loadedanim then loadedanim:Play() if themouse then themouse.Icon = "rbxasset://textures\\GunWaitCursor.png" wait(1) themouse.Icon = "rbxasset://textures\\GunCursor.png" end end end end end) tool.Equipped:connect(function(mouse) themouse = mouse humanoid = tool.Parent:FindFirstChild("Humanoid") if humanoid then local anim = tool:FindFirstChild("IdleAnim3") if anim and humanoid then if loadedidleanim then loadedidleanim:Stop() end loadedidleanim = humanoid:LoadAnimation(anim) if loadedidleanim then loadedidleanim:Play() end end local anim = tool:FindFirstChild("EquipAnim") if anim and humanoid then local loadedanim = humanoid:LoadAnimation(anim) if loadedanim then loadedanim:Play() end end end if themouse then themouse.Icon = "rbxasset://textures\\GunCursor.png" end end) tool.Unequipped:connect(function() if loadedidleanim then loadedidleanim:Stop() end end)

Local Script 2 slightly abbreviated:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local tool = script.Parent
local Damage = 1
local coolDown = 0.5
local range = 10
local bin = true


function isEquipped()
    if tool.Parent.Name ~= "Backpack" then
        return true
    else
        return false
    end
end

mouse.Button1Down:connect(function()
local tor = plr.Character.UpperTorso    
    if isEquipped() and bin then
        local target = mouse.Target
        if target.Parent.Name == NAME then
 if (target.Position-tor.Position).magnitude <= range then
local Selec = Instance.new("SelectionBox")
        Selec.Adornee = target
        Selec.Color3 = Color3.new(0,1,1)
        Selec.Parent = target       

    Selec:Destroy() 
end 
        end
    end
end)

Server Script:

local tool = script.Parent

local speedboost = 1.5
local damage = 0
local swingtime = .5
local combowindow = .5


local debris = game:GetService("Debris")
local handle = tool:WaitForChild("Handle")
local event = tool:WaitForChild("RemoteEvent")
local slashsound = handle:WaitForChild("SlashSound")
local overheadsound = handle:WaitForChild("OverheadSound")
local lungesound = handle:WaitForChild("LungeSound")

local lastclick = tick()
local combo = 0
local hithumanoids = {}



handle.Touched:connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local TargetPlayer = game.Players:GetPlayerFromCharacter(humanoid.Parent)
    if equipped and character and humanoid and humanoid.Health > 0 and hit and not hit:isDescendantOf(character) then
        if  humanoid and  humanoid.Health > 0 and not hithumanoids[humanoid] and (TargetPlayer and TargetPlayer.TeamColor ~= player.TeamColor or not TargetPlayer)
            then
            hithumanoids[ humanoid] = true

            for _, v in pairs( humanoid:GetChildren()) do
                if v and v.Name == "creator" then
                    v:remove()
                end
            end
            local tag = Instance.new("ObjectValue")
            tag.Name = "creator"
            tag.Value = player
            debris:AddItem(tag, 3)
            tag.Parent =  humanoid
             humanoid:TakeDamage(damage * (combo + 1))
            else

        end 
    end 
end
end)


tool.Activated:connect(function()
    local clickdelta = tick() - lastclick
    if clickdelta > swingtime then
        lastclick = tick()
        hithumanoids = {}
        if clickdelta < swingtime + combowindow then
            combo = (combo + 1) % 3
        else
            combo = 0
        end
        if player then
            if combo == 0 then
                event:FireClient(player, "RunAnimation", "SlashAnim2")
                slashsound:Play()
            elseif combo == 1 then
                event:FireClient(player, "RunAnimation", "ThrustAnim2")
                overheadsound:Play()
            elseif combo == 2 then
                event:FireClient(player, "RunAnimation", "OverheadAnim2")
                lungesound:Play()
            end
        end
    end
end)


tool.Equipped:connect(function()
    equipped = true
    lastclick = tick()
    combo = 0
    character = tool.Parent
    player = game.Players:GetPlayerFromCharacter(character)
    humanoid = character:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.WalkSpeed = humanoid.WalkSpeed * speedboost
    else
        character = nil
    end
end)

tool.Unequipped:connect(function()
    equipped = false
    if humanoid then
        humanoid.WalkSpeed = humanoid.WalkSpeed / speedboost
    end
    character = nil
    humanoid = nil
end)

How would I incorporate this service? Thanks!

0
Turn on repeat OnaKat 444 — 4y
0
Can you elaborate more please? I've not seen this used for UserInputService. Never2Humble 90 — 4y
0
OnaKat may be referring to enabling Loop for you animation. If you play the animation through Button1Down then it will continue forever. Just wait for the Button to go up and turn off the animation. alphawolvess 1784 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

you basically need to stop the loop when the mouse button up


local buttonDown = true function onButton1Down(mouse) buttonDown = true while buttonDown do -- do things wait() end end function onButton1Up(mouse) buttonDown = false end
0
Hi! Thank you for the response, I've tried incorporating that, and it did not work. I went and updated my main post with all the scripts in this, the first local script and the server script are basic tool scripts for swinging and causing damage, the second local script is a bit different, and its abbreviated for length but does other things aside from damage. Any thoughts on how I should- Never2Humble 90 — 4y
0
- incorporate what you responded with above into my scripts? Never2Humble 90 — 4y
Ad

Answer this question