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

How do I make it so that the function does not end?

Asked by 5 years ago
tool = script.Parent
Handle = tool.Handle

if tool.Activated then
    Handle.Touched:Connect(function(Touch)
        if Touch then
            L = Touch.Parent:FindFirstChild('Humanoid')
            if L then
                L:TakeDamage(69)
            end
        end
    end)
end

aight so im trying to make a weopon where if you click and it touches a person it does damage but there seems to be no way for me to press and damage keep in mind i dont wanna damage a person if I don't click

0
If you have any more code, care to show it? Add print() in areas, it's helpful. DaggerOf_Fly -24 — 5y

1 answer

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

Activated is an event. You can :Connect() to it,* but it is not a boolean. (This code will always run If you want to keep track of whether it's activated or not, you can set a value whenever it's Activated or Deactivated.

* You just connect to an event by doing event:Connect(function), so an example would be

local myFunction(thingThatGotItsAncestryChanged, newAncestor)
    print("how'd " .. thingThatGotItsAncestryChanged .. " get into " .. newAncestor)
end

local x = game.AncestryChanged:Connect(myFunction)

You can find more information on events here, the wiki has some good information on them. ("Discussion" is the important part since you don't need to disconnect anything, just connect.)

Edit: Another thing to note is that when you connect a function to a connection, it will always be connected unless directly disconnected (or the object of the event is destroyed since there's not going to be anything firing the event). This means that your current code will always check for when the handle is touched, even if the mouse button is no longer being held down. You can only stop an event by :Disconnecting it, meaning you should check inside the connected Touched function whether your mouse button is being held or not. (Preferable to do this by checking the variable that you're changing whenever a tool is activated or deactivated; disconnecting and reconnecting whenever the mouse button is pressed and unpressed is probably not very efficient.)

Edit 2: Edited the edit for clarification.

Ad

Answer this question