I know how to trigger a function when a part is touched, but how can I record how long a player has been standing on a part.
I'm not sure where to start.
part = workspace.Part --Or the part you're working with part.Touched:connect(function(hit) if not game.Players:GetPlayerFromCharacter(hit.Parent) then return end local char = hit.Parent local t1 = tick() part.TouchEnded:connect(function(hit) if hit.Parent == char then local t2 = tick() print(t2 - t1) end end) end)
So, we first get the part we want to touch in a variable. We then wait for someone to step on that part. When someone steps on that part, we're making sure that it is in fact a ROBLOX user that's touching the part. If not, let's not do anything. Otherwise we're going to store his character model in a variable called "char". Here's where tick() comes in. What tick() does is it gets the time, in seconds, since January 1st, 1970. What we can do with that is, we can find differences in time. We store the first time in a variable called "t1". We then wait for the guy to get off the part. When he does, we're going to make sure that it's the same guy getting off the part. If it is, we're going to get the time again, and print out the difference in time.
This can be accomplished by using the Touched
and TouchEnded
event.
ObjectName.Touched:connect(function(Hit) return print(Hit.Name) end) ObjectName.TouchEnded:connect(function() return print("Off") end)
But that's not it, We want to check if the Hit
parameter is a player, We can do that by checking if the player has a Humanoid
.
Object.Touched:connect(function(Hit) if Hit.Parent:FindFirstChild("Humanoid") then return Hit end end)
Next a loop, A loop is needed to continue the counting, 1,2,3,4,etc. In the Lua programming language there are many loops
such as : for,while, and repeat loop, But in this case, a While
loop will work, because the While
loop continues looping till the current Condition is false or nil
.
local count=0 Object.Touched:connect(function(Hit) if Hit.Parent:FindFirstChild("Humanoid") then while wait(1) do count=count+1 end end end)
But if the player loses interaction, the timer continues, Thats where the TouchEnded
event comes in handy. I'll also use a Boolean(true,false)
local count = 0 local PlayerOnPart = false Object.Touched:connect(function(Hit) PlayerOnPart = true if Hit.Parent:FindFirstChild("Humanoid") then while wait(1) and PlayerOnPart do count = count+1 end end end) Object.TouchEnded:connect(function() count = 0 PlayerOnPart = false end)
I'm not 100% sure on how the code would look (I really do JS and C++); but if you start out by defining a variable and having that be for the time, then once the player touches the part, have it trigger a while loop that triggers a running sum on the time variable.
Using funyun's idea, I can make is shorter.
local touch = false local Time = 0 script.Parent.Touched:connect(function(p) if p.Parent:FindFirstChild("Humanoid") then touch = true Time = 0 repeat wait(1) Time = Time+1 until not touch print(Time) --Time is how long they took. end end) script.Parent.TouchEnded:connect(function(p) if p.Parent:FindFirstChild("Humanoid") then touch = false end end)
Hope it helps!