So basically, I want to make a GUI show how many players are standing on a block. So I decided to give it a try, but it didn’t turn out to work that well. robloxapp-20211024-1318058.wmv (1.4 MB)
As you can see in the video, when I step on the block, the number goes up to 18, even though I am only one player. When I step back off, it goes back to zero. And the same thing happens over and over. I will admit, it looks pretty cool, but not what I am trying to achieve.
There is probably more efficient ways of doing what you want but, I recommend creating a BoolValue inside the player and when the player is touching it, it’ll set the BoolValue to true, that will prevent players from touching it multiple times.
local hello = script.Parent
plrtouch = 0
hello.Touched:connect(function(otherPart)
if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player touching the part
plrtouch = plrtouch + 1
script.Parent.BillboardGui.TextLabel.Text = plrtouch
end
end)
hello.TouchEnded:connect(function(otherPart)
if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player no longer touching the part
plrtouch = plrtouch - 1
script.Parent.BillboardGui.TextLabel.Text = plrtouch
end
end)
You just needed to add a check to see that it is a player touching the part, since non-player parts can cause the Touched event to fire.
Its pretty simple, you made a script which will add the number of parts hit
Your legs,hands and all other body stuff, counts as 18
Try this script below:
local playerstuching = 0
local tableofplayers = {}
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not table.find(tableofplayers,player.Name) then
table.insert(tableofplayers,player.Name)
playerstuching += 1
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if table.find(tableofplayers,player.Name) then
table.insert(tableofplayers,player.Name)
playerstuching += 1
end
end
end)
local hello = script.Parent
local debounce = false
plrtouch = 0
hello.Touched:connect(function(otherPart)
if debounce then
debounce = false
task.wait(5)
return
end
debounce = true
if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player touching the part
plrtouch = plrtouch + 1
script.Parent.BillboardGui.TextLabel.Text = plrtouch
end
end)
hello.TouchEnded:connect(function(otherPart)
if debounce then
debounce = false
task.wait(5)
return
end
debounce = true
if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player no longer touching the part
plrtouch = plrtouch - 1
script.Parent.BillboardGui.TextLabel.Text = plrtouch
end
end)
More info:
You can change task.wait(5) to a shorter length of time, this will restrict the amount of times a player can trigger both events.
If the player hits the brick twice that must mean that in between they stopped hitting the brick, hence the touchended event would fire and the counter would be reduced by 1. The debounce prevents the functions connected to both of the events from being executed too frequently.
local hello = script.Parent
local debounce = false
local debounce2 = true
plrtouch = 0
hello.Touched:connect(function(otherPart)
if debounce then
debounce = true
debounce2 = false
task.wait(5)
return
end
debounce = true
if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player touching the part
plrtouch = plrtouch + 1
script.Parent.BillboardGui.TextLabel.Text = plrtouch
end
end)
hello.TouchEnded:connect(function(otherPart)
if debounce2 then
debounce2 = true
debounce = false
task.wait(5)
return
end
debounce2 = true
if otherPart.Parent:FindFirstChild("HumanoidRootPart") then --is a player no longer touching the part
plrtouch = plrtouch - 1
script.Parent.BillboardGui.TextLabel.Text = plrtouch
end
end)
Check the logic inside the code, although I agree a players array is best suited for this use case. In the above a player can’t trigger the touched event twice in a row before triggering a touchended event, similarly this works the other way around too. Again “task.wait(5)” should be changed to a shorter time length.
I used your script, with a few modifications for it to show on the billboard gui
local playerstuching = 0
local tableofplayers = {}
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not table.find(tableofplayers,player.Name) then
table.insert(tableofplayers,player.Name)
playerstuching += 1
script.Parent.BillboardGui.TextLabel.Text = playerstuching
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if table.find(tableofplayers,player.Name) then
table.insert(tableofplayers,player.Name)
playerstuching += 1
script.Parent.BillboardGui.TextLabel.Text = playerstuching
end
end
end)