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)
this is bad because if the script is longer it can disconnect it and then everything after or before will stop
can you understand what I mean or should I explain deeper?
Can you explain deeper please, I don’t really understand what you want.
Just put this code in the PlayerAdded
event
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)
I’m not understanding what you want, can you explain in simpler terms or as steps for what is needed?
I made an example script for this, It will only run once for every player that’s close to the part.
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!")
---untill it does disconnect it will find more player this means " Has hit the base plate " will print more than once
---TweenService etc...
connection:Disconnect()
end
end)
Just add an extra check too
local base = game.Workspace:WaitForChild("Baseplate")
local connection
local touched = false
connection = 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!")
---untill it does disconnect it will find more player this means " Has hit the base plate " will print more than once
---TweenService etc...
connection:Disconnect()
end
end)
That’s not what will happen, it’ll disconnect almost immediately in most cases, depending if there going to be waits in your code, but if you want to be extra secure, do what @XdJackyboiiXd21 suggested by adding a debounce as well
Have you even tried this script I first mentioned, it should work for every individual player
(Ignore the AmountOfTimesTouched
variable I’ll fix it later)
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)
I think they want other players to be able to run this as well. For example, if a player touches the part, the event will fire, and if they touch it again, they won’t be able to fire it.
Ofcourse it will not immediately disconnect if a tween or other functions are before the disconnect. Also a debounce woudnt help here - it will help but still buggy.
Why won’t it. It’s not really a debounce, just an extra check that stops the function from running a second time.