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

How do I detect when a player touches a part?

Asked by 4 years ago

I want to make something where when a player walks into a certain area, a part stops being transparent. How do I do this? I'm new to scripting, so could you please send the whole script? Thanks in advance.

3 answers

Log in to vote
1
Answered by 4 years ago

Every Part has a Touched event. In order to run a few lines in your script every time a player touches a part, you just connect that event to a function. That would look like so:

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

end)

For that to work your script would have to be a child of the part you want the player to touch. If you look inside of the parentheses you will see that I put "hit". Whatever word you put in the parentheses (it doesn't have to be hit) will be a variable containing the object that touched the part. This will be useful to check if it was a player touching it before running a code, lets do that:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") == true then
        -- do this
    end
end)

Now every time an object touches your part, the script will run and check if the object was a player. If this is the case then your script will run. This is great and all, however if you leave it like this then you will run into a problem. Due to the fact that a player touches a part an infinite amount of times from when the touch begins to when the touch ends, the script will also run a lot of times. To fix this we can add what is referred to as a debounce. This would look like the script below:

local debounce = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") == true and debounce ~= true then
        debounce = true
        -- do this
        wait(RechargeTime) -- change that to a number of your choice
        debounce = false
    end
end)

What we have done there is created a variable named debounce which is set to false. When the part is touched we then check if a player touched it, and if the debounce is not equal to true (~= means not equal to). If you get past those checks then it will change the debounce to true which prevents the script from running again until it is false. You then run your script, wait however long you want, and set debounce back to false. That's all there is to it.

Hope I helped!

Ad
Log in to vote
0
Answered by 4 years ago

The most simple way would be to use BasePart.Touched.

function touch() --Put this in the part

    script.Parent.Transparency = 0

end
script.Parent.Touched:Connect(touch)
Log in to vote
0
Answered by 4 years ago

Do this




script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then script.Parent.Transparency = 0 end end)
0
How do I do this on one player's end? MinecraftButterfly87 10 — 4y
0
Put it in a Local Script. HyenaTheShovelX 15 — 4y

Answer this question