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

Script not working like I want , how to make something unanchored when somebody hits it ?

Asked by 6 years ago
Edited by Shawnyg 6 years ago

Hello , I'm getting mad because I'm trying to do something but , I just don't know how to script ;-; , I thought that this script would have worked , welp it's working but like , I don't need to touch it that it will break :

function x(hit)
script.Parent.Anchored = false
script.Parent.GlassSound:Play()
script:remove()
end

script.Parent.Touched:connect(x)

I'm asking for your help , I want that when somebody hit the block , it's unanchoring . Thanks ;-;

0
Edit: Put code in a code block Shawnyg 4330 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
function x(hit)
    if hit.Parent and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
        script.Parent.GlassSound:Play()

        for i,v in pairs(script.Parent:GetChildren()) do
            if v:IsA("BasePart") then
                v.Anchored = false
            end
        end

        script:Destroy()
    end
end

for i,v in pairs(script.Parent:GetChildren()) do
    if v:IsA("BasePart") then
        v.Touched:Connect(x)
    end
end

Line 2 will check to make sure it's actually a player that touched the table

Lines 5-8 will run through everything in the table model, if it's a part then it will unanchor it

And lines 15-16 runs through every part in the model, and line 17 waits for it to be touched

0
Oh hi friend Shawnyg 4330 — 6y
0
hi User#1007 6 — 6y
Ad
Log in to vote
0
Answered by
zblox164 531 Moderation Voter
6 years ago

This should work:

script.Parent.Touched:Connect(function(hit) -- Make sure you use Connect instead of:
    -- connect
    if hit.Parent.Name == "Humanoid" then
        script.Parent.Anchored = false
        script.Parent.GlassSound:Play()
    else
        return hit
    end
end)

So you saying that it's working but you don't need to touch the brick for it to be called? If so than the code above should solve you problem.

0
"if hit.Parent.Name == "Humanoid" then" It's impossible for a humanoid to touch something.. User#1007 6 — 6y

Answer this question