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

How come my keybinds wont work for my r15 shield script?

Asked by 4 years ago

So I have a shield script that I modified to work in r15, but since I don't know much about hot keybinds work I thought it would be fine if I did not change them, but since they don't work I'm looking for plenty of help on how keybinds work and how I could fix the keybind issue.

-------------[|Script|]----------------

wait(.1)

Player = game.Players.LocalPlayer
Character = Player.Character
LeftHand = Character:FindFirstChild("LeftHand")
UpperTorso = Character:FindFirstChild("UpperTorso")
UpperLeftArm = UpperTorso:FindFirstChild("UpperLeftArm")

ShieldTemplate = script:WaitForChild("ShieldTemplate")
VariantShield = script:WaitForChild("VariantShield")
RemoveFunction = script:WaitForChild("RemoveShield")

Use = false
DataY = 0
Up = false
Down = false
Busy = false
AnimationSpeed = 10 -- Slower, smoother with more integers

ShieldLink = nil
Weld = nil
LeftShoulderMotor = nil

local function WeldDecoration(Variant, WeldTo)
    local g = Variant:Clone()
    g.Parent = WeldTo.Parent
    local C = g:GetChildren()
    for i = 1, #C do
        if C[i]:IsA("BasePart") then
            C[i].CanCollide = true
            local W = Instance.new("Weld")
            W.Part0 = g.Middle
            W.Part1 = C[i]
            local CJ = CFrame.new(g.Middle.Position)
            local C0 = g.Middle.CFrame:inverse()*CJ
            local C1 = C[i].CFrame:inverse()*CJ
            W.C0 = C0
            W.C1 = C1
            W.Parent = g.Middle
        end

        local Y = Instance.new("Weld")
        Y.Part0 = WeldTo
        Y.Part1 = g.Middle
        Y.C0 = CFrame.new(0, 0, 0)
        Y.Parent = Y.Part0
    end

    local h = g:GetChildren()
    for i = 1, # h do
        h[i].Anchored = false
        h[i].CanCollide = false
    end
end

local function CreateShield()
    local Shield = ShieldTemplate:Clone()
    Shield.Name = "Shield"
    Shield.Parent = Character
    WeldDecoration(VariantShield, Shield.Handle)
    Shield:MoveTo(Character.UpperTorso.Position)
    ShieldWeld = Instance.new("Weld", Shield)
    ShieldWeld.Part0 = LeftHand
    ShieldWeld.Part1 = Shield.Handle

    local ShieldTag = Instance.new("StringValue", Shield.Handle)
    ShieldTag.Name = "IsShield"
    ShieldTag.Value = "Wood"

    ShieldWeld.C0 = CFrame.new(-0.175, -0.1, 0) * CFrame.fromEulerAnglesXYZ(math.rad(90),math.rad(90), 0)
    ShieldLink = Shield
end

function CommandIssued(Type, Data)
    if Type == "KeyPress" and Data ~= nil then
        local Key = Data:lower()
        if Key == "r" then
            if not Use and not Busy then
                Busy = true
                if not Character:FindFirstChild("Shield") then
                    CreateShield()
                end
                LeftShoulderMotor = UpperTorso["UpperLeftArm"].C1 -- Save data
                UpperLeftArm.Part1 = nil -- Disconnect arm from shoulder
                Weld = Instance.new("Weld") -- New Weld LeftArm to Torso
                Weld.Name = "LeftHand Weld"
                Weld.Part0 = UpperTorso
                Weld.Part1 = LeftHand
                Weld.Parent = UpperTorso
                Weld.C0 = CFrame.new(-1.5,0,0) -- Reposition arm

                for i = 1,AnimationSpeed do
                    Weld.C0 = Weld.C0 * CFrame.new(0.5/AnimationSpeed,-0.25/AnimationSpeed,-1/AnimationSpeed)*CFrame.fromEulerAnglesXYZ(math.rad(70/AnimationSpeed),math.rad(-40/AnimationSpeed),math.rad(40/AnimationSpeed)) -- if you tweak the numbers long enough you manage to get a smooth animation, sometimes it takes really long until you finally get all the positions right but when it comes to cframe/Weld manipulation the best way to achieve the wanted formula is trough trial and error
                    wait()
                end

                Busy = false
                Use = true
            else
                if not Busy then
                    Busy = true
                    if DataY > 0 then
                        for i = 1, DataY do
                            DataY = DataY - 1
                            Weld.C0 = Weld.C0 * CFrame.new(0,0,0.04)*CFrame.fromEulerAnglesXYZ(math.rad(-3),math.rad(1),math.rad(0))
                            if ShieldWeld ~= nil then
                                ShieldWeld.C0 = ShieldWeld.C0 * CFrame.new(.02, 0, .015) * CFrame.fromEulerAnglesXYZ(math.rad(.9), math.rad(-2), math.rad(.75)) -- SHIELD WELD HERE
                            end
                            wait()
                        end
                    end
                    for i = 1, AnimationSpeed do
                        Weld.C0 = Weld.C0*CFrame.new(-0.5/AnimationSpeed,0.25/AnimationSpeed,1/AnimationSpeed)*CFrame.fromEulerAnglesXYZ(math.rad(-70/AnimationSpeed),math.rad(40/AnimationSpeed),math.rad(-40/AnimationSpeed)) -- the equiping animation reversed
                        wait()
                    end
                    ShieldWeld.C0 = CFrame.new(-0.175, -0.1, 0) * CFrame.fromEulerAnglesXYZ(math.rad(90),math.rad(90), 0)
                    Weld:Destroy()
                    UpperLeftArm.Part1 = LeftHand
                    UpperLeftArm.C1 = LeftShoulderMotor
                    DataY = 0
                    Use = false
                    Busy = false
                end 
            end
        elseif Key == "r" then
            if Up == false and Down == false and Use == true and Busy == false then
                Up = true
                while Up == true and DataY < 30 do
                    DataY = DataY + 1
                    Weld.C0 = Weld.C0 * CFrame.new(0, 0, -0.04) * CFrame.fromEulerAnglesXYZ(math.rad(3),math.rad(-1),math.rad(0))
                    if ShieldWeld ~= nil then
                        ShieldWeld.C0 = ShieldWeld.C0 * CFrame.new(-.02, 0, -.015) * CFrame.fromEulerAnglesXYZ(math.rad(-.9), math.rad(2), math.rad(-.75)) -- SHIELD WELD HERE
                    end
                    wait()
                end 
            end
        elseif Key == "t" then
            if Down == false and Up == false and Use == true and Busy == false then
                Down = true
                while Down == true and DataY > 0 do
                    DataY = DataY-1
                    Weld.C0 = Weld.C0 * CFrame.new(0, 0, 0.04) * CFrame.fromEulerAnglesXYZ(math.rad(-3),math.rad(1),math.rad(0))
                    if ShieldWeld ~= nil then
                        ShieldWeld.C0 = ShieldWeld.C0 * CFrame.new(.02, 0, .015) * CFrame.fromEulerAnglesXYZ(math.rad(.9), math.rad(-2), math.rad(.75)) -- SHIELD WELD HERE
                    end
                    wait()
                end 
            end
        end
    elseif Type == "Remove" then
        if Weld then
            Weld:Destroy()
        end
        UpperLeftArm.Part1 = LeftHand
        if ShieldLink then
            ShieldLink:Destroy()
        end
    end
end

function CommandStopped(Key)
    Key = Key:lower()
    if Key == "i" then
        Up = false
    elseif Key == "u" then
        Down = false
    end
end

function RemoveFunction.OnClientInvoke()
    CommandIssued("Remove")
end

CreateShield()

Player:GetMouse().KeyDown:connect(function(Key)
    CommandIssued("KeyPress", Key)
end)

Player:GetMouse().KeyUp:connect(CommandStopped)

----[|Scripts Children|]----

A remote function named "RemoveShield" A model named "ShieldTemplate" A model named "VariantShield"

1 answer

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

Assuming you have a local script under the tool, you can get the players mouse like this:

--Local script under the tool
local player = game:GetService('Players').LocalPlayer -- Gets the player
local mouse = player:GetMouse() -- Gets mouse

script.Parent.Equipped:connect(function()
mouse.KeyDown:connect(function(Key)
    if Key == 'r' then -- If the key is r then
        Key:lower()
        print('key')
        script.Parent.Handle.BrickColor = BrickColor.Random() -- The handle will change color
    end
end)
end)

I hope this example helps you. If not please notify me.

Edit: I made some modifications to the script so it can have a forcefield function when a player presses the key 'R' so you can get a better understanding(just in case)

local player = game:GetService('Players').LocalPlayer
local mouse = player:GetMouse()

script.Parent.Equipped:connect(function()
mouse.KeyDown:connect(function(Key)
    if Key == 'r' then
        Key:lower()
        print('key')
        script.Parent.Handle.BrickColor = BrickColor.Random() --Handle changes color
        local shield = Instance.new('ForceField') --Makes a new instance, which is a forcefield
        shield.Name = 'Shield'
        shield:Clone().Parent = player.Character --We clone the shield into the players character for protection
        mouse.KeyUp:connect(function(Key) --function is called when the player lets the key up
            if Key == 'r' then --if the key they let go is R then
                Key:lower()
                local sh = player.Character:WaitForChild('Shield')
                sh:Destroy() --Shield is destroyed
            end
        end)
    end
end)
end)
0
Under code 07 you can add any function you would like other than a brick color changing AntoninFearless 622 — 4y
0
It seems like it wont work I think it might be because two main models for how the shield will look are not referenced, and I forgot to say the Shield its self is a model that with the original script it would be on the players arm. vincentthecat1 199 — 4y
0
It seems like it wont work I think it might be because two main models for how the shield will look are not referenced, and I forgot to say the Shield its self is a model that with the original script it would be on the players arm. vincentthecat1 199 — 4y
0
It seems like it wont work I think it might be because two main models for how the shield will look are not referenced, and I forgot to say the Shield its self is a model that with the original script it would be on the players arm. vincentthecat1 199 — 4y
0
If you can make a video link to your problem to help me better understand that would be appreciated AntoninFearless 622 — 4y
Ad

Answer this question