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

I can only shoot a rope once but cant shoot it again, why? [SOLVED]

Asked by 6 years ago
Edited 6 years ago

I have a script of a grapple hook thingy.. that shoots rope constraint and you can use it to swing. I want to do some kind of lava level where you need to swing above the lava without dying (falling). But for some reason when I shoot the rope, it shoots it only ONCE.. and I cant shoot it again.

I placed the code inside LocalScript and placed it inside StarterCharacterScripts.

Oh and also, when I was in the game I tried to delete the script inside my character and put it back in, and I could shoot the rope again. I thought of making a script that gives me a new one and deletes the old one, I dont really know how to do it. I wanted to know if there is an "easier" fix to this problem.

--// Variables:

local plr = game.Players.LocalPlayer
local char = plr.Character
local head = char:FindFirstChild("Head")

local athA = Instance.new("Attachment", head)
local athPart = game.Workspace.AttachPart
local athB = Instance.new("Attachment", athPart)
athA.Position = Vector3.new(0,0,-1)
local rope = Instance.new("RopeConstraint", head)
local length = (head.Position - athPart.Position).magnitude
local uis = game:GetService("UserInputService")
isSwinging = false


--// Functions:

uis.InputBegan:connect(function(key)
    if key.KeyCode == Enum.KeyCode.R  then
        if isSwinging == false then
            isSwinging = true
            rope.Attachment0 = athB
            rope.Attachment1 = athA
            rope.Length = length
            rope.Visible = true
        end
    end
end)

uis.InputEnded:connect(function(key)
    if key.KeyCode == Enum.KeyCode.R then
        if isSwinging == true then
            rope.Attachment0:Destroy() -- remove it
            rope.Attachment1:Destroy() -- remove it
            rope:Destroy()
            isSwinging = false
        end
    end
end)

https://vimeo.com/user90972315/review/297152618/40319cd20c

Video of me showing how I am removing and adding back the script, and it works.

Found the solution, I removed the lines that destroy the attachments

1
Do you have a debounce of when you can shoot or not? Life if you are loaded and when you are unloaded. Stephenthefox 94 — 6y
1
I mean, I made the "isSwinging".. I dont know if its correct tho and if it works. Is is good? HeyItzDanniee 252 — 6y
0
I got it. User#21908 42 — 6y
0
The length also needs set every time. User#21908 42 — 6y
View all comments (2 more)
0
I edited my answer, take a look. User#21908 42 — 6y
0
I already fixed it. Your code doesnt work, you should leave the attachments and not destroy them. Thank you anyways for trying when nobody else really did besides a few. :) HeyItzDanniee 252 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

The issue is that you are destroying the rope you made. The simple answer is to just create a new rope every time the player hits r. Example with comment explanation:

local players = game:GetService("Players") -- this is the recommended way to get the players service
local plr = players.LocalPlayer

local char = plr.Character
local head = char:FindFirstChild("Head")

local athA = Instance.new("Attachment") -- the second parameter to Instance.new() is deprecated. Set the parent later after all the properties have been changed to your liking.
local athPart = game.Workspace.AttachPart
local athB = Instance.new("Attachment")
athB.Parent = athPart 
athA.Position = Vector3.new(0,0,-1)
athA.Parent = head
local rope -- we will define this later
local length -- also define this later
local uis = game:GetService("UserInputService")
isSwinging = false

--// Functions:

uis.InputBegan:Connect(function(key) -- Use :Connect() not :connect(). :connect is deprecated
    if key.KeyCode == Enum.KeyCode.R  then
        if isSwinging == false then
            isSwinging = true
            rope = Instance.new("RopeConstraint") -- this will create a new rope every
 time the event is fired
            length = (head.Position - athPart.Position).magnitude
            rope.Attachment0 = athB
            rope.Attachment1 = athA
            rope.Length = length
            rope.Visible = true 
            rope.Parent = head -- setting the parent after all the properties are changed
        end
    end
end)

uis.InputEnded:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.R then
        if isSwinging == true then
            rope.Attachment0:Destroy()
            rope.Attachment1:Destroy()
            rope:Destroy() -- destroying the rope object that the variable rope refers to. The input began function above will redefine the variable rope and create a new rope object
            isSwinging = false
        end
    end
end)

I hope this helps and have a great day scripting! Edit: I changed where the length is defined as well. If this code still does not work, then the issue is with your variable logic and how you are using the different properties. I cannot help you beyond this because I don't know what your game and workspace look like.

0
Doesn`t works :C still same problem, I can only shoot once. HeyItzDanniee 252 — 6y
Ad
Log in to vote
-2
Answered by 6 years ago

Dude, you made a common mistake alot of people make. Whenever you destroy a object, or delete a object in your code, make sure to add it back. You destroyed the rope inside your code so when you launch the rope, there is no rope to launch. You destroy the rope here: (Line 36)

    if key.KeyCode == Enum.KeyCode.R then

        if isSwinging == true then

            rope.Attachment0:Destroy()

            rope.Attachment1:Destroy()

            rope:Destroy()

Either need to create a new rope in 'InputBegan' event or not destroy to rope in general and temporarily remove the attachment points.

Please accept this responce, i need to get rep to 100 again to talk in forum again cause moderators dont trust me. -.-

0
You don't always need to add something you destroyed back. User#21908 42 — 6y

Answer this question