I need help with Interaction system

Hello everyone, I’ve been making a simple interaction system to use in my games but i always getting the same problem while trying to solve problem

    1. What do I need to achieve? – I need to solve this strange problem so i’ll know what’s wrong with my script
    1. What is the issue? When doing an interaction, if player stopped looking at or stopped interacting before it should end, GUI stops to show to the player when player is looking at the Interactable object.
    1. What solutions have you tried so far? I haven’t seen any solutions so far, so i hope you will help me : D
      P.S. Except to some variables to be useless since i tried fixind code somehow
      THE LOCAL SCRIPT:
local Inter = game:GetService("ReplicatedStorage").Inter
local timeawait = 0
local done = false
local UserInputService = game:GetService("UserInputService")
local CanInteract = false
local target
local TS = game:GetService("TweenService")
local maingui = script.Parent
local Crosshair = maingui.Crosshair
local UpTitle = maingui.objectcontent:FindFirstChild("TextLabel")
local DownTitle = maingui.Interactcontent:FindFirstChild("TextLabel")
local Interacting = false
local CanResume = true
local ET
local still
local EstimatedTime = 1
local wastedtime = 0
local looking = false
local Progress = maingui.Progress
local Progressbar = Progress.Progressbar
local RunService = game:GetService("RunService")
local Info = TweenInfo.new(EstimatedTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
local Tween = TS:Create(Progressbar, Info, {
	Size = UDim2.new(1,0,1,0)
})
local function showup()
	UpTitle.TextTransparency = 0
	DownTitle.TextTransparency = 0
	Progress.BackgroundTransparency = 0
	Progressbar.BackgroundTransparency = 0
end
local function hide()
	Tween:Cancel()
	Progressbar.Size = UDim2.new(0, 0, 1, 0)
	UpTitle.TextTransparency = 1
	DownTitle.TextTransparency = 1
	Progress.BackgroundTransparency = 1
	Progressbar.BackgroundTransparency = 1
end
local function GetOrigin()
	local Mousepos = UserInputService:GetMouseLocation()
	local start = workspace.CurrentCamera:ViewportPointToRay(Mousepos.X, Mousepos.Y)
	return start
end
local function getDirection()
	local Direction = GetOrigin().Direction * 3
	return Direction
end
local function raycasting()
	local rcresult = workspace:Raycast(GetOrigin().Origin, getDirection())
	if rcresult then
		local testvar = rcresult.Instance.Parent.Setup
		if testvar then
			showup()
			looking = true
			if Interacting == false and looking == true then
				still = true
			end
			EstimatedTime = testvar.Hold.Value
			target = rcresult
			return rcresult
		else
			hide()
			looking = false
		end
	else
		hide()
		looking = false
	end
end
local holdkeydown = coroutine.create(function()
	while true do
		if wastedtime == EstimatedTime then
			Inter:FireServer(target.Instance)
			hide()
			break
		elseif Interacting == false then
			wastedtime = 0
			Tween:Cancel()
			break
		else
			wait(1)
			print("Iteration")
			wastedtime += 1
		end
	end
end)
UserInputService.InputEnded:Connect(function(InputObject, gameProcessedEvent)
	if gameProcessedEvent then return end
	if InputObject.KeyCode == Enum.KeyCode.F then
		Interacting = false
		hide()
		wastedtime = 0
		coroutine.yield(holdkeydown)
		CanResume = true
		print("inp ended")
	end
end)
UserInputService.InputBegan:Connect(function(InputObject, gameProcessedEvent)
	if gameProcessedEvent then return end
	if looking == true then
		if InputObject.KeyCode == Enum.KeyCode.F then
			Interacting = true
			if CanResume == true then
				coroutine.resume(holdkeydown)
			end
			CanResume = false
			Info = TweenInfo.new(target.Instance.Parent.Setup.Hold.Value, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
			Tween = TS:Create(Progressbar, Info, {
				Size = UDim2.new(1, 0, 1, 0)
			})
			still = false
			Tween:Play()
		end
	end
end)
RunService.RenderStepped:Connect(function()
	if Interacting == false then
		if still == true or looking == false then
			GetOrigin()
			getDirection()
			raycasting()
		end
	end
	done = false
end)

Thanks to anyone, who will help me.

Could you elaborate a little more on your issue? I am kinda having trouble understanding…

1 Like

Of course, there is a video about this issue!
robloxapp-20230917-1757063.wmv
P.S. - if i stop interacting before it should end, i can’t interact anymore.

any errors? You seem to still be interacting with it even when you are not hovering over it

No errors at all without including raycast founding settings in instance for tween (It’s not problem)

I think i should try :IsKeyDown() method, maybe it will work.