Desk tweenservice not working

local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")

local TweenInfo = TweenInfo.new(
    1,
    Enum.EasingStyle.Linear,
    Enum.EasingDirection.Out,
    0,
    false
)

local function PlayOpenAnimation(open)
    local goal = {
        Position = open.Position + Vector3.new(0, 0, -5)
    }
    local tween = TweenService:Create(open, TweenInfo, goal)
    tween:Play()
end

local function PlayCloseAnimation(open)
    local goal = {
        Position = open.Position - Vector3.new(0, 0, -5)
    }
    local tween = TweenService:Create(open, TweenInfo, goal)
    tween:Play()
end

for _, v in pairs(workspace:GetChildren()) do
    if CollectionService:HasTag(v, "Desk") then
        local open = v:FindFirstChild("Desk")
        if open then
            local ProximityPrompt = open:FindFirstChild("Front").ProximityPrompt
            if ProximityPrompt then
                local isOpen = false

                ProximityPrompt.Triggered:Connect(function()
                    if isOpen then
                        PlayCloseAnimation(open)
                        isOpen = false
                    else
                        PlayOpenAnimation(open)
                        isOpen = true
                    end
                end)
            end
        end
    end
end

not working, nothing happend when proximityprompt triggered
localscript at starterplayerscript, i want it to only apply for client side

Looking at your code:

Youā€™re using CollectionService to find all objects tagged with the Desk tag but then youā€™re looking for the first child named ā€œDeskā€ inside those models. However, looking at the Explorer hierarchy picture you added - there isnā€™t anything named ā€œDeskā€ inside the ā€œDeskā€ model which is what Iā€™m assuming youā€™ve tagged with Desk.

Other than that, I would recommend using the Instance signals to make sure that you manage all tagged instances as described here - avoids issues in StreamingEnabled environments and/or if you add/remove these items at runtime.

Also, might be a good idea to wrap that .ProximityPrompt in ::FindFirstChild call +/- a conditional just to be safe and error resistant.

2 Likes

first of all wth is this:


for _, v in pairs(workspace:GetChildren()) do
    if CollectionService:HasTag(v, "Desk") then
        local open = v:FindFirstChild("Desk")
        if open then
            local ProximityPrompt = open:FindFirstChild("Front").ProximityPrompt
            if ProximityPrompt then
                local isOpen = false

                ProximityPrompt.Triggered:Connect(function()
                    if isOpen then
                        PlayCloseAnimation(open)
                        isOpen = false
                    else
                        PlayOpenAnimation(open)
                        isOpen = true
                    end
                end)
            end
        end
    end
end

Instances have built-in has tag methods, you donā€™t need collection service for that:

local myPart = Instance.new("Part");
print(myPart:IsA("Part"));

You can get all instances with the given tag using this instead:

CollectionService:GetTagged("YOUR TAG");

Also you can mash close and open functions into one and that just makes the script less longā€¦
Like so:

local function SetDeskOpenState(open, isOpen: boolean)
  local offset = isOpen and Vector3.new(0, 0, -5) or Vector3.new(0, 0, 5);--ternary operator my beloved :)
  local goal =  {Position = open.Position + offset};

  TweenService:Create(open, TweenInfo, goal):Play();
end

So your script becomes:

local CollectionService = game:GetService("CollectionService");
local TweenService = game:GetService("TweenService");
local Info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out) --the rest is default values

local function SetDeskOpenState(open, isOpen: boolean)
  local offset = isOpen and Vector3.new(0, 0, -5) or Vector3.new(0, 0, 5);--ternary operator my beloved :)
--your last implementation was + -5 and - -5, using math we can deduce that + -5 is just -5 and --5 is just + 5
--so we can add open.Position + offset where offset is either -5(open) or 5(close)
  local goal =  {Position = open.Position + offset};

  TweenService:Create(open, Info, goal):Play();
end

local function AddDesk(desk)
  local open = desk:FindFirstChild("Open");--desk has a child named 'open' which is probably what you were looking for instead :)
  if not open then return end
 
  local front = open:FindFirstChild("Front");
  if not front then return end --don't forget, front can be nil too!

  local prox = front:FindFirstChildWhichIsA("ProximityPrompt");
  if not prox then return end

  local isOpen = false;
  prox.Triggered:Connect(function(plr)
    isOpen = not isOpen;--reverse the open boolean
    SetDeskOpenState(isOpen);--play the anim for the reversed value.
  end)
end

local function RemoveDesk(desk)
  --handle custom removing logic here...
end

CollectionService:GetInstanceAddedSignal("Desk"):Connect(AddDesk);
CollectionService:GetInstanceRemovedSignal("Desk"):Connect(RemoveDesk);
for _, desk in ipairs(CollectionService:GetTagged("Desk")) do--GetTagged() returns all instances with the given tag.
   AddDesk(desk);
end

This handles objects being added with the given tag, aswell as any objects currently tagged with it.
And it hopefully fixes your bug by changing ā€˜deskā€™ to ā€˜openā€™ because of the hierarchy.

hope this helps.

1 Like

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