ChosenTarget changes pos too frequently & is not visible

Code:

local plr = game.Players.LocalPlayer

local Targets = script.Parent.Targets

local BaseFrame = script.Parent.BaseFrame

local ProfileDisplays = BaseFrame.TopFrame.ProfileDisplays

local PlrLongestSession = plr.leaderstats.LongestSession

local function formatTime(seconds)
	
	local hours = math.floor(seconds / 3600)
	
	seconds = seconds % 3600
	
	local minutes = math.floor(seconds / 60)
	
	seconds = seconds % 60

	return string.format("%02d:%02d:%02d", hours, minutes, seconds)
	
end

local function SetTargetPos()
	
	local RandomX = Random.new():NextNumber(0.025,0.975)
	
	local RandomY = Random.new():NextNumber(0.058,0.942)
	
	print(RandomX)
	
	print(RandomY)
	
	return UDim2.new(RandomX,0,RandomY,0)
	
end

PlrLongestSession:GetPropertyChangedSignal("Value"):Connect(function()
	
	ProfileDisplays.LongestSessionDisplay.Text = "⏱" .. formatTime(PlrLongestSession.Value)
	
end)

local CurrentStreak = 0

local CurrentDifficulty = 0

local AvailableTargets = {
	"Square";
	"Circle";
	"Triangle";
};

local GameAreaFrame = BaseFrame.GameAreaFrame

local DifficultyDisplay = GameAreaFrame.DifficultyDisplay

local ChosenTarget = Targets:GetChildren()[math.random(1,#Targets:GetChildren())]
ChosenTarget.Parent = GameAreaFrame

ChosenTarget.Visible = true

local DifficultyData = require(game:GetService("ReplicatedStorage").DifficultyData)

local WaitInterval = nil

local TimeLeft = false -- time left to click target

local TS = game:GetService("TweenService")

local TweenStyle = TweenInfo.new(0.1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)

ChosenTarget.MouseButton1Click:Connect(function()
	
	if TimeLeft then
		
		local Tween = TS:Create(ChosenTarget,TweenStyle,{Size = UDim2.new(0,0,0,0)})
		Tween:Play()
		
		Tween.Completed:Wait()
		
		TimeLeft = false
		
	end
	
end)

while true do
	
	for Difficulty,Data in pairs(DifficultyData) do
		
		if WaitInterval ~= Data.TimeInterval then WaitInterval = Data.TimeInterval end
		
		if math.clamp(CurrentStreak,Data.MinStreak,Data.MaxStreak) == CurrentStreak and DifficultyDisplay.Text ~= Difficulty then
			
			DifficultyDisplay.Text = Difficulty
			
			DifficultyDisplay.TextColor3 = Data.DifficultyColor
			
			ChosenTarget.ImageColor3 = Data.DifficultyColor
			
		end
		
	end
	
	local ChosenTargetPos = SetTargetPos()
	
	ChosenTarget.Position = ChosenTargetPos
	
	print(ChosenTarget.Position)
	
	TimeLeft = true
	
	for secs = WaitInterval,0 do
		
		if TimeLeft then
			
			ChosenTarget.DespawnTimer.Text = tostring(math.floor(secs * 100 + 0.5) / 100)
			
		else
			
			break
			
		end
		
		task.wait(0.01)
		
	end
	
	TimeLeft = false
	
	local Tween = TS:Create(ChosenTarget,TweenStyle,{Size = UDim2.new(0,0,0,0)})
	Tween:Play()

	Tween.Completed:Wait()
	
end
2 Likes

a little bit more information might help us understand wth is happening lol

what is “ChosenTarget”, what is the script supposed to do, what are you trying to achieve, and why does your script consist of a function which formats time when your question is related to the position of something?

This script is supposed to make the targets appease in different position when clicked on (aim trainer) chosen target is a variable that chooses a random shape such as circle square or triangle

Most likely you don’t see the chosen Target because its size has changed to (0,0,0,0):

I have added a test solution:

if TimeLeft then
	local Tween = TS:Create(ChosenTarget, TweenStyle, {Size = UDim2.new(0, 0, 0, 0)})
	Tween:Play()
	Tween.Completed:Wait()

	ChosenTarget.Position = SetTargetPos()
	ChosenTarget.Size = UDim2.new(0.1, 0, 0.1, 0)  -- change size if necessary
	ChosenTarget.Visible = true

	TimeLeft = false
end

A more likely reason that chosen Target is changing its position quickly is that there are no delays in your loop.

while true do
	for Difficulty,Data in pairs(DifficultyData) do

		if WaitInterval ~= Data.TimeInterval then WaitInterval = Data.TimeInterval end

		if math.clamp(CurrentStreak,Data.MinStreak,Data.MaxStreak) == CurrentStreak and DifficultyDisplay.Text ~= Difficulty then

			DifficultyDisplay.Text = Difficulty

			DifficultyDisplay.TextColor3 = Data.DifficultyColor

			ChosenTarget.ImageColor3 = Data.DifficultyColor

		end

	end

	task.wait(WaitInterval) -- cooldown

	ChosenTarget.Position = SetTargetPos()

	TimeLeft = true

	for secs = WaitInterval, 0, -0.01 do
		
		if TimeLeft then
			
			ChosenTarget.DespawnTimer.Text = tostring(math.floor(secs * 100 + 0.5) / 100)
			
		else
			
			break
			
		end
		
		task.wait(0.01)
	end

	TimeLeft = false

	local Tween = TS:Create(ChosenTarget, TweenStyle, {Size = UDim2.new(0, 0, 0, 0)})
	Tween:Play()
	
	Tween.Completed:Wait()
end

There is a delay. The timer:

for secs = WaitInterval, 0, -0.01 do
		
		if TimeLeft then
			
			ChosenTarget.DespawnTimer.Text = tostring(math.floor(secs * 100 + 0.5) / 100)
			
		else
			
			break
			
		end
		
		task.wait(0.01)
	end

Your script previously had an incomplete “for” loop. Have you added -0.01 to your script as shown in my script?

for secs = WaitInterval, 0, -0.01 do

I think it was purposely originally not there

I didn’t really understand what you wanted to say, but if you replace the wait Interval with a number, then a warning immediately appears:
image