Get for how long a part exist

Hi devs !
I would like to know if it’s possible to get for how long a part exist cause in my game at a certain moment I clone a part and for another system I need to know since when the part exist !
Thanks you for the reply and have a nice day !

1 Like

You can use debris: Debris | Roblox Creator Documentation

1 Like

Directly - no. But you can create an Attribute or Values to validate.
Example:

local part = game.ReplicatedStorage.Part
local clonepart = part:Clone()
clonepart:SetAttribute("Created", os.clock())
clonepart.Parent = game.workspace --EXAMPLE!!

--to check how
local part = game.workspace:FindFirstChild("Part")
local createdat = part:GetAttribute("Created")
2 Likes

there’s probably an easier method but here’s a direct approach:
Place this in ServerScriptService

for i,v in pairs(game.Workspace:GetChildren()) do
if v:IsA("BasePart") or v:IsA("UnionOperation") then --or whatever you're looking for
local newscript = script.Counter:Clone()
newscript.Parent = v
local timealive = Instance.new("NumberValue")
timealive.Parent = game.ReplicatedStorage --or wherever you want
timealive.Name = v.Name
end

and place a script named Counter under the first script (we’re gonna use this so you can still access the value after the part is destroyed)

local timealive = 0
while true do
task.wait(1)
timealive+=1
game.ReplicatedStorage:FindFirstChild(script.Parent.Name).Value = timealive
end
1 Like

This is not simple at all, it is a difficult method that also creates an unnecessary burden!

3 Likes

To add on to this, to calculate the time elapsed, it’s a simple subtraction:

local createdAt = part:GetAttribute("Created")
local existedFor = os.clock() - createdAt
2 Likes
local times = {}
local function timer(object: Instance)
	times[object] = os.clock() -- store the time it is right now in a table
	return object
end
local function getLifeTime(object: Instance)
	return os.clock() - times[object] -- get the difference between the time now and the time stored
end

local part = timer(Instance.new("Part"))
part.Name = "hello"
part.Parent = workspace

task.wait(0.5)
print(getLifeTime(part)) -- ~ 0.5
task.wait(4)
print(getLifeTime(part)) -- ~ 4.5
1 Like

Thank you @RickAstll3y, @3YbuKtOp, @CircDev, @zamont124, and @majdTRM !
I will use the method of @3YbuKtOp !
But I don’t understand how debris could be useful in my case @zamont124 ?
Have a nice day !

There’s just one error in your script, you are setting attribute on the wrong part :

local part = game.ReplicatedStorage.Part
local newpart = part:Clone()
newpart:SetAttribute("Created", os.clock())
newpart.Parent = game.workspace --EXAMPLE!!

--to check how
local part = game.workspace:FindFirstChild("Part")
local createdat = part:GetAttribute("Created")
1 Like

Another solution is to create a wrapper for instancing parts/objects:

local TimedInstance = {}

function TimedInstance.new(name: string, parent: Instance?): Instance
	local obj = Instance.new(name, parent)
	obj:SetAttribute("Created", os.clock())
	return obj
end

return TimedInstance

That way you can require this from a different script and get rid of having to check for all the game parts but only check for the parts/objects you want:

--other script
local TimedInstance = require(script.TimedInstance) --assuming that's the location
local TimedPart = TimedInstance.new("Part", workspace) 
task.wait(5)
print(os.clock()-TimedPart:GetAttribute("Created")) --around 5
1 Like

Yes, you are right, something hurried, thanks!

1 Like

Thanks, uhh i have just understand wrongfully. Have a nice day. Also debris is removing part in given seconds not getting how long a part exist so not what you asked.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.