Help on scripting a door to swing outward (Based on where the player is facing)

Currently the code tweens just fine, but it only tweens in a certain direction. I want the door to swing outward based on the player who activates the code.

Current code when the proximityprompt is activated:

prompt.Triggered:Connect(function() -- On prompt use
			if debounce == false then
				debounce = true
				prompt.Enabled = false
				v.Main.CanCollide = false
				v.Main.Open:Play()
				if open == false then
					open = true
                    -- goal should be set depending on who activated the goal (-90 or 90)
					goal = {CFrame = currentCFrame * CFrame.Angles(0, math.rad(90), 0)} 
					-- Rotate 90 degrees from current rotation
					prompt.ObjectText = "Close Door"
				else
					open = false
                    -- goal should be reset back to original position
					goal = {CFrame = currentCFrame * CFrame.Angles(0, math.rad(0), 0)}
					-- Rotate back to starting rotation
					prompt.ObjectText = "Open Door"
				end

				local tween = tweenService:Create(hinge, tweenInfo, goal)
				tween:Play()
				tween.Completed:Wait() 
				debounce = false
				prompt.Enabled = true
				v.Main.CanCollide = true
			end
		end)

I already figured out part of the code to get where the player is viewing, but I’m not sure how to implement it into the existing server script and get the information of who first activated the door, so it will swing out from their direction.

-- Function to determine if a player is facing a specific direction
local function isFacing(player)
    local character = player.Character
    if character then
        local head = character:FindFirstChild("Head")
        if head then
            local lookVector = (part.Position - head.Position).unit
            local headLookVector = head.CFrame.lookVector
            local dotProduct = lookVector:Dot(headLookVector)
            return dotProduct > 0.5 -- Adjust the threshold as needed for accuracy
        end
    end
    return false
end
2 Likes

may I ask what outward would be like if the player was facing towards the door

like push or pull

The problem with using the player’s head direction is what if they are on the ‘inside’ of the door and facing inward? The door would swing in.
You could shoot a Shapecast from one side of the door (let’s say 10 studs away, facing towards the ‘inside’ of the door) that ignores everything but a Humanoid.

  • If the Shapecast hits the Humanoid in the 0-9 stud range the player is on the inside, so tween it outwards.
  • If the Shapecast hits the Humanoid in the 11-20 stud range the player is outside, so tween in inwards.

I’ve given a 2 stud range in the middle just so a player standing against the door doesn’t cross over the split at 10 studs and cause the hit to be registered on both sides.

4 Likes

Uhh what if given the case if there are two players on both sides of the door, how would that work then?

1 Like

how about 2 proximity prompts, one on each side

1 Like

The shapecast would only be used by the script with the ProximityPrompt that fired. The script checks the result of the shapecast for the player that fired it. In the case of 2 players firing very close to the same time then you might get issues with the door tweening one direction because of the first one firing it, then redirecting when the second fires it, but a debounce would fix that to ignore any inputs. You can disable the Prompt while it’s doing it’s job and after the tween plays then enable the Prompt again.

1 Like

Not sure I want to do this approach, could cause some issues if the prompt on the other side was detected / pressed.

1 Like

You can use the dot product of the players root part and the door to determine what side they are on and rotate the door based off that.

local doorProduct = (--[[your door]].Position - rootPart.Position).Unit:Dot(--[[your door]].CFrame.LookVector)

if doorProduct > 0 then
     --rotate door 90
else
     --rotate door -90
end
2 Likes

Maybe I’ll try this, totally slipped my mind that you can get the player info from the promityprompt.

ProximityPrompt.Triggered:Connect(function(player)
   -- blah blah blah
end)
1 Like

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