Touchevent: How to make a touchevent with just 1Player?

Hey, please help me!

  1. What do you want to achieve? Keep it simple and clear!
    I want make a touchevent with just 1 Player. Like when he hits the part then it does print(“1 time hitted”…player.Name") the problem is you cant use

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried but it does print more than once

I think this can be solved with tables

    local Players = game:GetService('Players')
    local base = game.Workspace.Baseplate
    local touching
    Players.PlayerAdded:Connect(function(player)
    	local char = player.Character or player.CharacterAdded:Wait()
    	local touched = false 

    	touching = base.Touched:Connect(function(hit)
    		if hit.Parent == char then
    			touched = true
    			print("is touching")
    			if touched == true then
    				touching:Disconnect()
    			end
    		end
    	end)
    	
    end)

Couldn’t you just also check if touched is equal to false in your if statement?

local Players = game:GetService('Players')
local base = game.Workspace.Baseplate

Players.PlayerAdded:Connect(function(player)
    local AmountOfTimesTouched = 0
    local char = player.Character or player.CharacterAdded:Wait()
    local touched = false 

    base.Touched:Connect(function(hit)
    	if hit.Parent == char and touched == false then
    		touched = true
            AmountOfTimesTouched += 1
    		print(player.Name.."'s times that he/she hit the part: "..tostring(AmountOfTimesTouched))
        end
    end)
end)

Maybe you could do something relevant to this

and what if player touchs the part at the same time? It seems like then it wont work or?

I’m not sure how this would work, but you can use the TouchEnded Event as well to detect when the Character has stopped touching the part & set the touched variable back to false

I don’t think I understood that, do you mean when every player in the game attempts to touch the part at once?

I mean when every player in the game attempts to touch the part. It could be end like it does print more than once. I want a unbuggy touch event script.

Could you show me how you would do that with touchEnded?

Do not rely on .TouchEnded. It’s unstable and doesn’t work at all times.

1 Like

Can you also give us more details on what you want to achieve? Do you want the player to only be able touch the part once?

Since the Touched Event is handled inside the PlayerAdded, it’ll only fire once for every individual player

Also @TheRealANDRO, is TouchEnded wonky due to the fact that it’s relying on a ton of physics that’s being detected? If so, maybe FindPartsInRegion3 could be a possible alternate?

.TouchEnded is unreliable due to the physics and hitboxes of the characters, and it causes a ton of performance issues.

I’m suggesting you use .magnitude, it’s easier and it’s more reliable since it doesn’t cause too many performance issues.

1 Like
local base =  game.Workspace:WaitForChild("Baseplate")

local MainTable = {}

game.Players.PlayerAdded:Connect(function(player)
      MainTable[player.Name] = 0  
end)
base.Touched:Connect(function(hit)
      if hit.Parent:FindFirstChild("Humanoid") then
            MainTable[hit.Parent.Name] += 1
            print(hit.Parent.Name.." Has hit the base plate "..MainTable[hit.Parent.Name] .." Times!")
      end
end)

It worked for me:
https://gyazo.com/12dcc33ac6836ef4b2d60538efcd7c33

Können Sie uns auch näher erläutern, was Sie erreichen möchten? Möchten Sie, dass der Spieler den Part nur einmal berühren kann?:

yes exactly

but I want just one time and not many times

What do you mean? You want it to only print once?

Something like this?

local base =  game.Workspace:WaitForChild("Baseplate")

local MainTable = {}

base.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not MainTable[hit.Parent.Name] then
		MainTable[hit.Parent.Name] = true
		print(hit.Parent.Name.." Has hit the base plate!")
	end
end)

This should only run once for each player

Idk why you replied to me but ok

Apparently the OP wants to include a touched event that only fires once, I don’t know how else we can really manage this

again the player can just hit the part once. Like when too many people hit it then it will find the first player

Wait so you want it to register 1 hit then stop detecting hits?

local base =  game.Workspace:WaitForChild("Baseplate")
local touched = false

base.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not touched then
		touched = true
		print(hit.Parent.Name.." Has hit the base plate!")
	end
end)

Here’s an example script to detect the players that are close to the part.

local LoopTime = 1
local PlayerTouchedTable = {}
local Part = game.Workspace:WaitForChild("YourPartName") -- Enter your part name.

while wait(LoopTime) do
   for _, player in pairs(game.Players:GetChildren()) do
     if player.Character then
       if player.Character:FindFirstChild("HumanoidRootPart") then
         local Magnitude = (player.Character.HumanoidRootPart.Position - Part.Position).Magnitude
         if Magnitude <= 1 then
           if table.find(PlayerTouchedTable, player.Name) then
           else
             print("Player is close to the part!")
             table.insert(PlayerTouchedTable, player.Name)
           end
         end
      end
   end
end

That’s not what happens here, what happens in that is that it uses a table debounce, if wont have issues if many people touch at once.

It checks if the parent of hit has a humanoid, and then checks if they have a key in the table with their name, and if they don’t, give them a key with the value of true so no matter how many times you touch it, it will not detect you anymore as it will only care about you if you don’t have a key or that key’s value results in false

@XdJackyboiiXd21 wouldn’t it be better to just disconnect the event in that case?

local base =  game.Workspace:WaitForChild("Baseplate")
local connection

connection = base.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		print(hit.Parent.Name.." Has hit the base plate!")
		connection:Disconnect()
	end
end)
1 Like