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

How to know if the same player touched a part multiple times?

Asked by
CjayPlyz 643 Moderation Voter
5 years ago
Edited 5 years ago
script.Parent.Touched:connect(function(part)
    -- make sure the part still exists
    if not part.Parent then return end

    -- make sure it's a humanoid
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if not humanoid  then return end

    workspace.nocpp.Value = workspace.nocpp.Value - 1
end)

How do i prevent workspace.nocpp.value from negating if the part(the parent of the script) was touched by the same person twice or more?

2 answers

Log in to vote
1
Answered by 5 years ago

Thepoint13's answer will suffice, however you can also do it within the same script.

last = {} -- Last Player to touch
local debounce = false -- Debounce to prevent multiple touches at the same time.
script.Parent.Touched:connect(function(part)
    if debounce == false then
    -- make sure the part still exists
    if not part.Parent then return end

    -- make sure it's a humanoid
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if not humanoid  then return end
        debounce = true
    if part.Parent.Name ~= last then -- If Player wasn't the last player to touch then
        last = part.Parent.Name
        print(last, "Touched")
        wait(.5)
        debounce = false
else -- If the player was  the last person to touch then
print("Player already touched")
debounce = false
end
end
end)

One problem with this script however, is the player won't be able to touch the brick again until another player does. You could add a StringValue somewhere and make last = StringValue, then have a separate script reset the last touched every so often.

0
THANKYOU CjayPlyz 643 — 5y
0
You're welcome! :) WizyTheNinja 834 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

You can either put a local script inside the player with a variable, when the player touches, you change the value inside it. Use Fireserver() to do this.

You can also use tables. Use table.Import the players name.

0
kk CjayPlyz 643 — 5y

Answer this question