How do I add a delay to this script?

Hello everyone, I can’t change how to add a delay to my script. The delay should be after all gems are spawned.

local gui = script.Parent
local Gems = gui.ImageLabel
local GemsPosition = gui.GemsLable
local touchedPart = game.Workspace:WaitForChild("Part")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1)
local false1 = false
local goal = {
	Position = GemsPosition.Position
}

touchedPart.Touched:Connect(function(plr)
	local humanoid = plr.Parent:FindFirstChild("Humanoid")
	if humanoid and false1 == false then
		false1 = true
		for i = 1, 10 do
			local randomX = math.random(35, 65) / 100
			local randomY = math.random(35, 65) / 100
			local gemsClone = Gems:Clone()
			gemsClone.Parent = gui
			gemsClone.Position = UDim2.new(randomX, 0, randomY, 0)	
			local Tween = tweenService:Create(gemsClone, tweenInfo, goal)
			Tween:Play()
		end
		wait(3)
		false1 = false
	end
end) 

spawn the gems, and then wait, and then move them

wait() before you play the tween?

then the gems are moved one at a time, and I need them to move together.

This should work! :+1:

local gui = script.Parent
local Gems = gui.ImageLabel
local GemsPosition = gui.GemsLable
local touchedPart = game.Workspace:WaitForChild("Part")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1)
local false1 = false
local goal = {
	Position = GemsPosition.Position
}

touchedPart.Touched:Connect(function(plr)
	local humanoid = plr.Parent:FindFirstChild("Humanoid")
	if humanoid and false1 == false then
		false1 = true
        TweenTable = {}
		for i = 1, 10 do
			local randomX = math.random(35, 65) / 100
			local randomY = math.random(35, 65) / 100
			local gemsClone = Gems:Clone()
			gemsClone.Parent = gui
			gemsClone.Position = UDim2.new(randomX, 0, randomY, 0)	
			local Tween = tweenService:Create(gemsClone, tweenInfo, goal)
			table.Insert(TweenTable, Tween)
		end
                wait(1)
                for i, v in pairs(TweenTable) do
                       V:Play()
                end
		wait(2)
		false1 = false
	end
end)

I’ll check it out now!!!

1 Like

your script is not working!!!

Alright.

  1. What does happen?

  2. Are there any errors?

local gui = script.Parent
local Gems = gui.ImageLabel
local GemsPosition = gui.GemsLable
local touchedPart = game.Workspace:WaitForChild("part1")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1)
local false1 = false
local goal = {
	Position = GemsPosition.Position
}

touchedPart.Touched:Connect(function(plr)
	local humanoid = plr.Parent:FindFirstChild("Humanoid")
	if humanoid and false1 == false then
		false1 = true
		local TweenTable = {}
		for i = 1, 10 do
			local randomX = math.random(35, 65) / 100
			local randomY = math.random(35, 65) / 100
			local gemsClone = Gems:Clone()
			gemsClone.Parent = gui
			gemsClone.Position = UDim2.new(randomX, 0, randomY, 0)	
			local Tween = tweenService:Create(gemsClone, tweenInfo, goal)
			table.Insert(TweenTable, Tween)
		end
		wait(1)
		for i, v in pairs(TweenTable) do
		   v:Play()
		end
		wait(2)
		false1 = false
	end
end)

use table.insert, not table.Insert. Capitalization matters.

1 Like

thnaks bro!!!

You vould also use task.defer() it wants till everything in yhe connection runs the it will run

1 Like

Ah yes I see. Small error on my part lol. Glad its working!

1 Like

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