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

How do i change my code so that instead of pressing c you have to click with mouse?

Asked by 3 years ago
Edited 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

I want to make a code where a player can use a tool that activates when you have it out and left click to shoot out a electric bird in front them. I have a script that shoots out a electric bird when the player presses c while holding the tool.

In starter pack i have a tool call Thunder Bird. In the tool i inserted a string value called Active and a string value called Equip. I also have a local script in the tool.

--- Equip/UnEquip
local Tool = script.Parent

Tool.Equipped:Connect(function()
    Tool.Equip.Value = true
end)

Tool.Unequipped:Connect(function()
    Tool.Equip.Value = false
end)

--- Skill
local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()
local Debounce = true
Player = game.Players.LocalPlayer
Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://4608419697"

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.C and Debounce == true and Tool.Equip.Value == true and Tool.Active.Value == "None" then
        Debounce = false
        Tool.Active.Value = "HieShock"
        wait(0.1)
        Track1 = Player.Character.Humanoid:LoadAnimation(Animation)
        Track1:Play()
        wait(0.15)
        script.Fire:FireServer(plr)
    local hum =  Player.Character.Humanoid
    for i = 1,30 do
        wait()
        hum.CameraOffset = Vector3.new(
            math.random(-1,1),
            math.random(-1,1),
            math.random(-1,1)
        )
    end
    hum.CameraOffset = Vector3.new(0,0,0)
        wait(0.15)
        Tool.Active.Value = "None"
        wait(2)
        Debounce = true
    end
end)

in the local script i have a remote event named Fire and a script called Skill in the remote event Fire

script.Parent.OnServerEvent:Connect(function(plr)
    local Alive = true
    local Projectile = game.ReplicatedStorage.HieDF.HieShock.IceBird:Clone()
    Projectile.Parent = workspace
    Projectile.CanCollide = false
    Projectile.Anchored = false
    Projectile.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,4,0) * CFrame.fromEulerAnglesXYZ(0,0,0)
    Projectile.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name then
            hit.Parent.UpperTorso.Velocity = plr.Character.UpperTorso.CFrame.UpVector * 125
            local HitEffect = game.ReplicatedStorage.HieDF.HieShock.ShockwaveFX:Clone()
            local ShockwaveFX = game.ReplicatedStorage.HieDF.HieShock.ShockwaveFX:Clone()
            HitEffect.Parent = workspace
            HitEffect.CFrame = hit.Parent.HumanoidRootPart.CFrame
            ShockwaveFX.Parent = workspace
            ShockwaveFX.CFrame = hit.Parent.HumanoidRootPart.CFrame
            HitEffect.CanCollide = false
            HitEffect.Anchored = true
            hit.Parent.Humanoid:TakeDamage(5 + plr.Data.DevilFruit.Value)
            Projectile:Destroy()        
            for i = 1,25 do
                wait(.05)
                HitEffect.Size = HitEffect.Size + Vector3.new(0.5,0.5,0.5)
                HitEffect.Transparency = HitEffect.Transparency + 0.1
                ShockwaveFX.Transparency = HitEffect.Transparency + 0.1
            end 
            HitEffect:Destroy()
        end
        if hit.Parent:FindFirstChild("Enemy") and hit.Parent.Name ~= plr.Name and hit.Parent.Enemy.Health >= 0 and Alive == true then
            hit.Parent.UpperTorso.Velocity = plr.Character.UpperTorso.CFrame.UpVector * 125
            local HitEffect = game.ReplicatedStorage.HieDF.HieShock.ShockwaveFX:Clone()
            local ShockwaveFX = game.ReplicatedStorage.HieDF.HieShock.ShockwaveFX:Clone()
            HitEffect.Parent = workspace
            HitEffect.CFrame = hit.Parent.HumanoidRootPart.CFrame
            ShockwaveFX.Parent = workspace
            ShockwaveFX.CFrame = hit.Parent.HumanoidRootPart.CFrame
            HitEffect.CanCollide = false
            HitEffect.Anchored = true
            hit.Parent.Enemy:TakeDamage(5 + plr.Data.DevilFruit.Value)
            Projectile:Destroy()
            if hit.Parent.Enemy.Health <= 0 then
                Alive = false
                --Kill rewards
                local Leader = plr:FindFirstChild("Quest")
                plr.Data.Beli.Value = plr.Data.Beli.Value + game.ReplicatedStorage.KillRewards:FindFirstChild(hit.Parent.Name).Beli.Value
                plr.Data.Exp.Value = plr.Data.Exp.Value + game.ReplicatedStorage.KillRewards:FindFirstChild(hit.Parent.Name).Exp.Value
                if Leader.Type.Value == game.ReplicatedStorage.KillRewards:FindFirstChild(hit.Parent.Name).Type.Value then
                if Leader ~= nil then
                Leader.Amount.Value = Leader.Amount.Value + 1
                Alive = true    
              end
             end
            end         
            for i = 1,25 do
                wait(.05)
                HitEffect.Size = HitEffect.Size + Vector3.new(0.5,0.5,0.5)
                HitEffect.Transparency = HitEffect.Transparency + 0.1
                ShockwaveFX.Transparency = HitEffect.Transparency + 0.1
            end 
            HitEffect:Destroy()
        end
    end)
    local BV = Instance.new("BodyVelocity",Projectile)
    BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    BV.Velocity = plr.Character.HumanoidRootPart.CFrame.LookVector * 110
    wait(0.75)
    BV:Destroy()
    wait(0.25)
    Projectile:Destroy()
    end)

In replicated storage i have a folder called HieDf and in that folder i have another folder called HieShock that has a mesh part called IceBird that is the bird that gets shooten.

0
I don't think this is your code, if you're asking how to change such a small portion of it Gey4Jesus69 2705 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You need to get the mouse, and use the .Button1Down event:

local uis = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")
local mouse = player:GetMouse()

local deb

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://4608419697"

local tool = script.Parent

tool.Equipped:Connect(function()

    tool.Equip.Value = true

end)

tool.Unequipped:Connect(function()

    tool.Equip.Value = false

end)

mouse.Button1Down:Connect(function()

    if not deb then return end
    if not tool.Equip.Value then return end
    if tool.Active.Value ~= "None" then return end

    deb = true
    tool.Active.Value = "HieShock"

    wait(0.1)

    local track1 = player.Character.Humanoid:LoadAnimation(anim)
    track1:Play()

    wait(0.15)

    script.Fire:FireServer()

    for i = 1,30 do

        wait()

        humanoid.CameraOffset = Vector3.new(
            math.random(-1,1),
            math.random(-1,1),
            math.random(-1,1)
        )

    end

    humanoid.CameraOffset = Vector3.new(0,0,0)

    wait(0.15)

    tool.Active.Value = "None"

    wait(2)

    deb = false

end)

Hope this helped!

Ad
Log in to vote
1
Answered by 3 years ago

As zamd157 stated, Button1Down is deprecated, instead we are going to use MouseButton1 of UIS.


local uis = game:GetService("UserInputService") local player = game.Players.LocalPlayer local char = player.Character local humanoid = char:WaitForChild("Humanoid") local mouse = player:GetMouse() local deb local anim = Instance.new("Animation") anim.AnimationId = "rbxassetid://4608419697" local tool = script.Parent tool.Equipped:Connect(function() tool.Equip.Value = true end) tool.Unequipped:Connect(function() tool.Equip.Value = false end) uis.InputBegan:Connect(function(key) if key.UserInputType == Enum.UserInputType.MouseButton1 then -- If left click is clicked if not deb then return end if not tool.Equip.Value then return end if tool.Active.Value ~= "None" then return end deb = true tool.Active.Value = "HieShock" wait(0.1) local track1 = player.Character.Humanoid:LoadAnimation(anim) track1:Play() wait(0.15) script.Fire:FireServer() for i = 1,30 do wait() humanoid.CameraOffset = Vector3.new( math.random(-1,1), math.random(-1,1), math.random(-1,1) ) end humanoid.CameraOffset = Vector3.new(0,0,0) wait(0.15) tool.Active.Value = "None" wait(2) deb = false end end)

Good luck, if you encounter problems, comment down so I can check.

0
Your choice if you want to use Button1Down, but it's deprecated, however, CAS and UIS are not. Gabe_elvin1226aclan 323 — 3y
0
When i left click it shows 00:49:07.316 - Players.Pullingajash.Backpack.Thunder Bird.HieShock:5: attempt to index nil with 'WaitForChild' in output. Pullingajash 9 — 3y

Answer this question