Tween not playing

Hello there!

I have made a code that detects a door’s distance from the player. After the distance is less than 20, I used a tween to make it look like the door opened. But when I run the code, the tween does not play. I tested it with a few prints, and the code is definitely running. I used a normal script.

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local players = game.Players


local function tweenMove(target, time, place)
	local objectMove = TweenService:Create(target, TweenInfo.new(time, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0.1),{CFrame = place.CFrame})
	objectMove:Play()
	local completedEvent; completedEvent = objectMove.Completed:Connect(function(...)
		completedEvent:Disconnect()
	end)
end


RunService.Heartbeat:Connect(function()
	for i, player in pairs(players:GetPlayers()) do
		if player.Character then
			if player:DistanceFromCharacter(game.Workspace.Door.DoorPort.Position) < 20 then
				if script.Parent.DoorPort.CFrame ~= script.Parent.DoorCFrame.CFrame then
					tweenMove(script.Parent.DoorPort, 0.5, script.Parent.DoorCFrame)
				end
			else
				if script.Parent.DoorPort.CFrame ~= script.Parent.DoorOgCFrame.CFrame then
					tweenMove(script.Parent.DoorPort, 0.5, script.Parent.DoorOgCFrame)
				end
			end
		end
	end
end)

Also, if anybody knows a way to optimize this code more, please let me know.

it works just fine when i removed 60% of your script

local TweenService = game:GetService("TweenService")
local players = game.Players


local function tweenMove(target, Time, place)
	local objectMove = TweenService:Create(target, TweenInfo.new(Time, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0.1),{CFrame = place.CFrame})
	objectMove:Play()
	local completedEvent; completedEvent = objectMove.Completed:Connect(function(...)
		completedEvent:Disconnect()
	end)
end
tweenMove(script.Parent.DoorPort, 0.5, script.Parent.Script.Parent)
1 Like

I want it to run once the player is a certain distance from the door.

ok i think i fixed it, maybe the run service is going too fast that the tween breaks

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local players = game.Players

local Debounce = true

local function tweenMove(target, Time, place)
	if Debounce == true then
		Debounce = false
		local objectMove = TweenService:Create(target, TweenInfo.new(Time, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0.1),{CFrame = place.CFrame})
		objectMove:Play()
		local completedEvent; completedEvent = objectMove.Completed:Connect(function(...)
			completedEvent:Disconnect()
		end)
		
		task.wait(Time)
		
		Debounce = true
	end
end


RunService.Stepped:Connect(function()
	for i, player in pairs(players:GetPlayers()) do
		if player.Character then
			if player:DistanceFromCharacter(game.Workspace.Door.DoorPort.Position) < 20 then
				print(player:DistanceFromCharacter(game.Workspace.Door.DoorPort.Position))
				if script.Parent.DoorPort.CFrame ~= script.Parent.DoorCFrame.CFrame then
					tweenMove(script.Parent.DoorPort, 0.5, script.Parent.DoorCFrame)
				end
			else
				if script.Parent.DoorPort.CFrame ~= script.Parent.DoorOgCFrame.CFrame then
					tweenMove(script.Parent.DoorPort, 0.5, script.Parent.DoorOgCFrame)
				end
			end
		end
	end
end)