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

What is wrong with this script?

Asked by
Ulysies 50
8 years ago

So I am attempting to create a climb script, its not working.. How would I fix this?

Player = script.Parent.Parent
Player.CharacterAdded:wait(1)
Char = Player.Character
Left, Right = Char:WaitForChild("Left Arm"), Char:WaitForChild("Right Arm")

--Put in starterpack

--Controlls: Q And E

Distance = 10 --How far they can reach

Can = true

Mouse = Player:GetMouse()

Mouse.KeyDown:connect(function(Key)
    if string.lower(Key) == "q" then
        local Q = Left:FindFirstChild("Q")
        if Q then
            Q:remove()
        end
        local Pos = Mouse.Hit.p
        if (Pos - Left.Position).magnitude < Distance then
        if Can then
        Can = false
        local Body = Instance.new("BodyPosition", Left)
        Body.Name = "Q"
        Body.maxForce = Vector3.new(20000, 20000, 20000)
        Body.position = Pos
        local Part = Instance.new("Part", Char)
        Part.Transparency = 1
        Part.FormFactor = "Custom"
        Part.Size = Vector3.new(1, 1, 1)
        local Box = Instance.new("SelectionBox", Part)
        Box.Adornee = Part
        Part.CFrame = CFrame.new(Pos)
        Part.Name = "Q"
        wait(0.25)
        Can = true
        end
        end
    end
    if string.lower(Key) == "e" then
        local Pos = Mouse.Hit.p
        local E = Right:FindFirstChild("E")
        if E then
            E:remove()
        end
        if (Mouse.Hit.p - Right.Position).magnitude < Distance then
        if Can then
        Can = false
        local Body = Instance.new("BodyPosition", Right)
        Body.Name = "E"
        Body.maxForce = Vector3.new(20000, 20000, 20000)
        Body.position = Pos
        local Part = Instance.new("Part", Char)
        Part.Transparency = 1
        Part.FormFactor = "Custom"
        Part.Size = Vector3.new(1, 1, 1)
        local Box = Instance.new("SelectionBox", Part)
        Box.Adornee = Part
        Part.CFrame = CFrame.new(Pos)
        Part.Name = "E"
        wait(0.25)
        Can = true
        end
        end
    end
end)

Mouse.KeyUp:connect(function(Key)
    if string.lower(Key) == "q" then
        local Q = Left:FindFirstChild("Q")
        local Q2 = Char:FindFirstChild("Q")
        if Q then
            Q:remove()
        end
        if Q2 then
            Q2:remove()
        end
    end
    if string.lower(Key) == "e" then
        local E = Right:FindFirstChild("E")
        local E2 = Char:FindFirstChild("E")
        if E then
            E:remove()
        end
        if E2 then
            E2:remove()
        end
    end
end)
0
Please be less vague in your titles and questions. What exactly isn't working? LittleBigKid2000 75 — 8y

1 answer

Log in to vote
0
Answered by
Wutras 294 Moderation Voter
8 years ago

It's most likely this part:

local Q = Left:FindFirstChild("Q")
        if Q then
            Q:remove()
        end

and this part:

 local E = Right:FindFirstChild("E")
        if E then
            E:remove()
        end

The problem with this is that if "Q" and "E" are nil(Which happens to be whenever the script runs the first time) then the script would break. So I'd propose this as solution:

Player = script.Parent.Parent
Player.CharacterAdded:wait(1)
Char = Player.Character
Left, Right = Char:WaitForChild("Left Arm"), Char:WaitForChild("Right Arm")

--Put in starterpack

--Controlls: Q And E

Distance = 10 --How far they can reach

Can = true

Mouse = Player:GetMouse()

Mouse.KeyDown:connect(function(Key)
    if string.lower(Key) == "q" then
        if Left:FindFirstChild("Q") then
            Left:FindFirstChild("Q"):remove()
        end
    local Q = Left:FindFirstChild("Q")
        local Pos = Mouse.Hit.p
        if (Pos - Left.Position).magnitude < Distance then
        if Can then
        Can = false
        local Body = Instance.new("BodyPosition", Left)
        Body.Name = "Q"
        Body.maxForce = Vector3.new(20000, 20000, 20000)
        Body.position = Pos
        local Part = Instance.new("Part", Char)
        Part.Transparency = 1
        Part.FormFactor = "Custom"
        Part.Size = Vector3.new(1, 1, 1)
        local Box = Instance.new("SelectionBox", Part)
        Box.Adornee = Part
        Part.CFrame = CFrame.new(Pos)
        Part.Name = "Q"
        wait(0.25)
        Can = true
        end
        end
    end
    if string.lower(Key) == "e" then
        local Pos = Mouse.Hit.p
        if Right:FindFirstChild("E") then
           Right:FindFirstChild("E"):remove()
        end
    local E = Right:FindFirstChild("E")
        if (Mouse.Hit.p - Right.Position).magnitude < Distance then
        if Can then
        Can = false
        local Body = Instance.new("BodyPosition", Right)
        Body.Name = "E"
        Body.maxForce = Vector3.new(20000, 20000, 20000)
        Body.position = Pos
        local Part = Instance.new("Part", Char)
        Part.Transparency = 1
        Part.FormFactor = "Custom"
        Part.Size = Vector3.new(1, 1, 1)
        local Box = Instance.new("SelectionBox", Part)
        Box.Adornee = Part
        Part.CFrame = CFrame.new(Pos)
        Part.Name = "E"
        wait(0.25)
        Can = true
        end
        end
    end
end)

Mouse.KeyUp:connect(function(Key)
    if string.lower(Key) == "q" then
        local Q = Left:FindFirstChild("Q")
        local Q2 = Char:FindFirstChild("Q")
        if Q then
            Q:remove()
        end
        if Q2 then
            Q2:remove()
        end
    end
    if string.lower(Key) == "e" then
        local E = Right:FindFirstChild("E")
        local E2 = Char:FindFirstChild("E")
        if E then
            E:remove()
        end
        if E2 then
            E2:remove()
        end
    end
end)

Hope this helps you. :)

Ad

Answer this question