Issues with TweenService on the Client

So, I’m using TweenService for a door that I’m making. Inside the door, there are two hinges, which are the objects being controlled by TweenService and rotated. Each hinge is attached to one side of the door, the left and the right. Originally, I had been tweening it on the server, however, I noticed that the faster you try to make the door, the more choppy, unrealistic, and messy it would look. Therefore, I moved it to the client controlled by a remote event.

The only problem with this is that when the door is open on everyone’s client, when a new player joins, the door appears closed for them. I’ve experimented with game.Players.PlayerAdded on the client and seeing if it was opened already, and then opening it for them if it’s closed on their client, but nothing like that seems to be working very well, and ends up breaking.

If there are any better solutions, please let me know! Would be greatly appreciated.

Server-Side Script

local status = closed

 script.Parent.Clicker.MouseClick:Connect(function(plr)
	if plr:GetRankInGroup(5033680) >= 25 then
	if status == "closed" then
		game.ReplicatedStorage.open:FireAllClients("open")
		status = "open"
		iconopen()
	elseif status == "open" then
		game.ReplicatedStorage.open:FireAllClients("close")
		status = "closed"
		iconclose()
		end
	end
end)

Client-Side Script

local TS = game:GetService("TweenService")

local hinge = game.Workspace.StaffDoors1.StaffAccessDoor.StaffDoor.Door.Hinge
local hinge2 = game.Workspace.StaffDoors1.StaffAccessDoor2.StaffDoor.Door.Hinge

local cf = Instance.new("CFrameValue")
cf.Value = hinge.CFrame
cf.Changed:Connect(function()
	hinge.CFrame = cf.Value
end)

local cf2 = Instance.new("CFrameValue")
cf2.Value = hinge2.CFrame
cf2.Changed:Connect(function()
	hinge2.CFrame = cf2.Value
end)

local waittime = 0.5

local status = "closed"

game.ReplicatedStorage.open.OnClientEvent:Connect(function(action)
	if action == "open" then
	status = "open"
	print("open")
	TS:Create(cf, TweenInfo.new(waittime), {Value = hinge.CFrame * CFrame.Angles(math.rad(0), math.rad(95), math.rad(0))}):Play()
	TS:Create(cf2, TweenInfo.new(waittime), {Value = hinge2.CFrame * CFrame.Angles(math.rad(0), math.rad(-95), math.rad(0))}):Play()
	elseif action == "close" then
	status = "closed"
	print("close")
	TS:Create(cf, TweenInfo.new(waittime), {Value = hinge.CFrame * CFrame.Angles(math.rad(0), math.rad(-95), math.rad(0))}):Play()
	TS:Create(cf2, TweenInfo.new(waittime), {Value = hinge2.CFrame * CFrame.Angles(math.rad(0), math.rad(95), math.rad(0))}):Play()
	end
end)

On the server listen for player added.

Players.PlayerAdded:Connect(function(player)
    remote:FireClient(player)
end)

Though this might seem unrealistic since it’s actually animating the door. You might want to have a boolean argument that determines whether to play the animation or move it instantly for newcomers.

1 Like

Oh. That was similar to what I was doing before, but I think my issue might have been that I was using :fireAllClients instead of :FireClient. I’ll try that out. Thanks.

Would you check if the player argument from the remote event is the LocalPlayer on the client-side script with an if statement, or would that not be necessary?

It would not be necessary since there is no case where it will fire to someone else.

Hm. It didn’t seem to work. I’m not sure if I got a full understanding of what you meant.

This was the code.

Server-Side

game.Players.PlayerAdded:Connect(function(plr)
	game.ReplicatedStorage.check:FireClient(plr)
end)

Client-Side

game.ReplicatedStorage.check.OnClientEvent:Connect(function(plr)
	if status == "open" then
		TS:Create(cf, TweenInfo.new(waittime), {Value = hinge.CFrame * CFrame.Angles(math.rad(0), math.rad(95), math.rad(0))}):Play()
	TS:Create(cf2, TweenInfo.new(waittime), {Value = hinge2.CFrame * CFrame.Angles(math.rad(0), math.rad(-95), math.rad(0))}):Play()
	end
end)

Nevermind. I fixed it. The issue was that I was checking to see if it was open on the client instead of on the server.