Help with scripting Trinket System

while wait(25) do
	local Despawn_Time = 50
	
for i,v in pairs(workspace.TrinketSpawn:GetChildren()) do
	local Trinket = game.ReplicatedStorage.Trinket:GetChildren()
	local randomItem = Trinket[math.random(1, #Trinket)]
	

	if v.Name == "Part" then
		if v.Transparency == 0.9 then return end
		v.Transparency = 0.9
		local Newrand = randomItem:Clone()
		Newrand.Parent = v
		
		Newrand.Handle.CFrame = v.CFrame
			
		print("Part spawned")
		wait(Despawn_Time)
		print("Part Despawned")
			if Newrand.Parent == v then
				Newrand:Destroy()
			end
		v.Transparency = 1
		end
	end
end

I want a trinket to spawn at a part every 25 seconds then if nobody picks it up after 50 seconds it despawns. The script works but it spawn a trinket 25 second then it wait until it despawn to spawn another trinket. Any help!??!?!?

2 Likes

the script repeats that script every 25 seconds but in the script it has wait(50) so it waits an additional 50 seconds before ending and repeating, try using a function instead and repeat that

local function SpawnTrinket(v)
	local Despawn_Time = 50
	local Trinket = game.ReplicatedStorage.Trinket:GetChildren()
	local randomItem = Trinket[math.random(1, #Trinket)]
	
	if v.Name == "Part" then
		if v.Transparency == 0.9 then return end
		v.Transparency = 0.9
		local Newrand = randomItem:Clone()
		Newrand.Parent = v

		Newrand.Handle.CFrame = v.CFrame

		print("Part spawned")
		wait(Despawn_Time)
		print("Part Despawned")
		if Newrand.Parent == v then
			Newrand:Destroy()
		end
		v.Transparency = 1
	end
end

while wait(25) do
	
	
for i,v in pairs(workspace.TrinketSpawn:GetChildren()) do

	
	SpawnTrinket(v)
	
	end
end

Still didnt work. Did I do it wrong?

maybe u can put the despawn part in a separate script in the trinket bc if ur storing it in anything but SSS or Workspace the script wont run so if u make a scrip in the trinket and simple just make it wait 50 seconds and then despawn the trinket, then u should be able to remove the despawn part in the other script. Cant help anymore i have to go so i wont be able to reply anymore.

1 Like
local trinkets = {}

local function SpawnTrinket(v)
	local Trinket = game.ReplicatedStorage.Trinket:GetChildren()
	local randomItem = Trinket[math.random(1, #Trinket)]

	if v.Name == "Part" then
		if v.Transparency == 0.9 then return end
		v.Transparency = 0.9
		local Newrand = randomItem:Clone()
		table.insert(trinkets, Newrand)
		Newrand.Parent = v

		Newrand.Handle.CFrame = v.CFrame
		
		task.wait(50) -- waits 50 seconds before deleting
		if Newrand.Parent == v and table.find(trinkets, Newrand) then
			table.remove(trinkets, Newrand)
			Newrand:Destroy()
		end
		v.Transparency = 1
	end
end

while task.wait(25) do -- this executes the spawnTrinket function every 25 seconds
	SpawnTrinket("your_path_here")
end
1 Like