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

3DMG - Having problems with a repeat function - Any help?

Asked by 9 years ago

Hey, guys.

I'm making a 3DMG from Attack on Titan, however it seems that my script is not working. I've scattered prints all over it and it seems that it won't come out of the repeat loop properly when the key comes up. Here's my script: Player = game.Players.LocalPlayer Mouse = Player:GetMouse() Torso = Player.Character.Torso UpdateGrapple = false Speed = 50 debounce = false

function Grapple(Location, MTar)
    if MTar == nil then return end
    Player.Character.Humanoid.Died:connect(function ()
        Container:Destroy()
        if Torso:FindFirstChild("RocketPropulsion") ~= nil then
            Torso.RocketPropulsion:Destroy()
        end
    end)
    Container = Instance.new("Model", game.Workspace)
    Container.Name = Player.Name.."Container"
    print("Firing grapple!")
    loc = Location.p
    distance = (loc - Torso.Position).magnitude
    print("Defined variables")
    Target = Instance.new("Part", Container)
    Target.Name = Player.Name.."Target"
    Target.Transparency = 0
    Target.Anchored = false
    print("Made target")
    W = Instance.new("ManualWeld", Target)
    W.Part0 = Target
    W.Part1 = MTar
    W.C0 = CFrame.new()
    W.C1 = MTar.CFrame-MTar.CFrame.p
    print("Welded target")
    Target.CFrame = Location
    RP = Instance.new("RocketPropulsion", Player.Character.Torso)
    RP.CartoonFactor = 1
    RP.Target = Target
    RP.MaxThrust = 10000
    RP.MaxTorque = Vector3.new(3000, 3000, 3000)
    RP.MaxSpeed = Speed
    RP:Fire()
    print("Made and fired RP. Starting the wire sequence")
    repeat
        Wire = Instance.new("Part", Container)
        Wire.Name = Player.Name.."Wire"
        Wire.BrickColor = BrickColor.new("Really black")
        Wire.Transparency = 0
        Wire.Anchored = true
        Wire.CanCollide = false
        Wire.TopSurface = Enum.SurfaceType.Smooth
        Wire.BottomSurface = Enum.SurfaceType.Smooth
        Wire.formFactor = Enum.FormFactor.Custom
        distance = (loc - Torso.Position).magnitude
        Wire.Size = Vector3.new(0.2, 0.2, distance)
        Wire.CFrame = CFrame.new(loc, Torso.CFrame.p) * CFrame.new(0, 0, -distance/2)
        wait()
        Wire:Destroy()
    until UpdateGrapple == false
    print("Wire sequence ended.")
    print("Unlatched!")
    Container:Destroy()
    RP:Destroy()
end

Mouse.KeyDown:connect(function (Key)
    if Key == "q" then
        UpdateGrapple = true
        Grapple(Mouse.hit, Mouse.Target)
        print("Latched!")
        Mouse.KeyUp:connect(function (Key)
            if Key == "q" then
                UpdateGrapple = false
                print("UpdateGrapple been set to false")
            end 
        end)
    end
end)

Basically you press Q, it shoots the wire and activates the RocketPropulsion object to send you towards the desired point, and starts updating the wire in real time so it looks like you're reeling it in. When the player releases Q, the requirement to stop the loop is met and it stops updating and deletes everything. Any help appreciated.

1 answer

Log in to vote
0
Answered by
MrFlimsy 345 Moderation Voter
9 years ago

It may take me a while to figure out the issue, but I see one unrelated issue in your script that you should probably change. You're declaring the KeyUp event every time the KeyDown event is triggered, meaning every time you press Q, it adds 1 to the amount of times your KeyUp event will trigger. Here's a quick fix:

Mouse.KeyDown:connect(function (Key)
    if Key == "q" then
        UpdateGrapple = true
        Grapple(Mouse.hit, Mouse.Target)
        print("Latched!")
    end
end)

Mouse.KeyUp:connect(function (Key)
    if Key == "q" then
        UpdateGrapple = false
        print("UpdateGrapple been set to false")
    end 
end)

Edit 1: Now that I've read your script, this might actually be the problem. I can't find anything else that could be the issue. Try it and tell me how it works.

Edit 2: Woo! I feel accomplished!

1
Actually, you accidentally fixed the issue in the process of fixing this one. I see now that it doesn't need to be inside the other function, because it cannot go up without going down first. Thank you! SquirreIOnToast 309 — 9y
Ad

Answer this question