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

Help with Touched function?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

What this script is suppose to do is when you sit down in the vehicle seat then you are suppose to have your body's transparency to == 1 then when you get up off the seat you will have your transparency to = 0

script.Parent.Touched:connect(function(hit)
     if hit.Parent.Humanoid.Sit == true then
        for i, p in pairs(hit.Parent:GetChildren()) do
            p.Transparency = 1
        end
    end
     if hit.Parent.Humanoid.Sit == false then
        for i, p in pairs(hit.Parent:GetChildren()) do
            p.Transparency = 0
        end
    end
end)
0
Did you try using a While loop that detects if the player is Sitting? Break the loop once he gets up and set the Transparency back after the loop Necrorave 560 — 8y
0
nope FiredDusk 1466 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

Ok, well first of all, you haven't checked to see if humanoid is actually existent nor whether the player exists. Also, in the for loop you haven't checked to see if p is a part. These could all lead to problems, for example if any part touched that, the script would be broken for the duration of the time that server's up, simply because the script returned an error because it was told that the part had a humanoid with it didn't! Here's the fixed code:

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil and game.Players:FindFirstChild(hit.Parent.Name) ~= nil then -- Check if there's a humanoid and the player exists.
        if hit.Parent.Humanoid.Sit == true then
            for i, p in pairs(hit.Parent:GetChildren()) do
                if p:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then -- Check if it's a part and isn't the humanoid root part.
                    p.Transparency = 1
                end
            end
        end
        if hit.Parent.Humanoid.Sit == false then
            for i, p in pairs(hit.Parent:GetChildren()) do
                if p:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then -- Check if it's a part and isn't the humanoid root part.
                    p.Transparency = 0
                end
            end
        end
    end
end)

If this works for you or helps you to understand please vote up and accept this as your answer.

0
Does not. FiredDusk 1466 — 8y
0
Who upvoted it, it doesn't work .-. User#5978 25 — 8y
Ad

Answer this question