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

Breaking door in touch tool script,Battering ram?

Asked by 5 years ago
Edited 5 years ago

Thats the script,it dont works and i dont know why,Anything is wrong? Its a normal script into a part of a tool.

local debounce = false 
dir = math.random(2)

function onTouch(part) 
if script.Parent.Parent.Swing.Value == true then
    if part.Name == "Door" then

                    sund = Instance.new("Sound",part)
                    sund.SoundId = "http://www.roblox.com/Asset/?ID=158712406"
                    sund.Volume = 2
                    sund:play()
                    part:BreakJoints()
                    part.Anchored = false
                    part.CanCollide = false
                    part.Velocity = part.CFrame.lookVector *10



            end
        end

wait(1) 
debounce = false 
end 



script.Parent.Touched:connect(onTouch) 
0
Do some debugging. Is the script a server or local? Local Scripts don't run in Workspace. Is the script disabled? Enable it. Add calls to the print function in your script. If it prints, t is in fact running. User#19524 175 — 5y
0
Are there any errors? yellp1 193 — 5y

1 answer

Log in to vote
0
Answered by
iTamago 18
5 years ago
Edited 5 years ago
debounce = false
dir = math.random(1,2)

script.Parent.Touched:Connect(function(part)
    if debounce == true then
        return
    else
        if script.Parent.Parent..Swing.Value == true and part.Parent.Name == "Door" then
            local sund = Instance.new("Sound",part)
            sund.SoundId = "rbxassetid://158712406"
            sund.Volume = 2
            sund:Play()
            for i,v in pairs(part.Parent:GetChildren())
                v.Anchored = false
                v.CanCollide = false
                for e,p in pairs(v) do
                    if p:FindFirstChild("WeldConstraint") then
                        p:Destroy()
                    end
                end
                v.Velocity = v.CFrame.lookVector * 10
            end
        end
    wait(1)
    debounce = false
    end
end)

Breakdown of the script:

math.random(1,2)

math.random() needs a minimum and maximum value to calculate between.

script.Parent.Touched:Connect(function(part)

When an object touches the script's parental object, it will identify that object with the parameter called "part" for the function (not the entire script).

if debounce == true then
    return
else

When the variable is fired already, it will not trigger again until the variable debounce is equal to false.

if script.Parent.Parent..Swing.Value == true and part.Parent.Name == "Door" then

If both of these variables are what they should be, then the function will continue. Otherwise, it will not continue until both of them are what they should be.

Assuming that the script's parental object touched a part that is inside a model, I added part.Parent to get the model name instead. You can change this back if the part is not in a model, or if you are looking for a part inside a model.

for i,v in pairs(part.Parent:GetChildren())
    v.Anchored = false
    v.CanCollide = false
    for e,p in pairs(v) do
        if p:FindFirstChild("WeldConstraint") then
            p:Destroy()
        end
    v.Velocity = v.CFrame.lookVector * 10
    end
end

For every part in the model, it will unanchor, make uncollideable, and then later, have the part fly towards it's lookVector.

The velocity may not be what you want it to be, because if the lookVector of a part is facing towards the battering ram, it will fly that way instead. To fix this, you just have to reface each part towards a direction.

For each item's class named "WeldConstraint", it will destroy that. You may change this to whatever fits your game, but since it is a door, I assumed that it is a WeldConstraint.

wait(1)
debounce = false

After it is done with destroying each constraint, it will let anything else fire after 1 second.

Let me know how it goes. If possible I'd like to test it as well if you'd like.

Ad

Answer this question