Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

2 comments

0

THANKYOU

0

You're welcome! :)

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.

1 comment

0

kk

This discussion is preserved as it was. New answers, comments, and votes are closed.