Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

force player to face a certain direction when on a part?

Asked by 3 years ago

So i basically just need the player to face a certain direction whenever he/she is touching a part. Also the player will not be able to move (they are anchored) when on the part. any help or advice?

1 answer

Log in to vote
0
Answered by
sayer80 457 Moderation Voter
3 years ago
Edited 3 years ago
local adjustment = 90 -- deegrees the player should turn around adjust it
local adjustmentorientation = CFrame.Angles(0,math.rad(adjustment),0) -- putting it into a CFrame component

local Part = script.Parent -- the touched part, should be over the actual part invisible and cancollide = false!
local deb = false -- debounce
Part.Touched:Connect(function(touched) -- when part gets touched it fires
    if deb then return end -- end function when debounce 
    deb = true -- debounce activated
    local hum = touched.Parent:FindFirstChild("Humanoid") -- get rootpart returns nil/the humanoid
    local rootpart = touched.Parent:FindFirstChild("HumanoidRootPart") -- get rootpart returns nil/the part
    if hum ~= nil then -- check if humanoid is there
        if rootpart ~= nil and rootpart.Anchored == false then -- checks if player is already anchored and rootpart not nil
            rootpart.CFrame = Part.CFrame*adjustmentorientation -- Tps Player to the Part you have to adjust the Part height
            --^^ you can also just add up the height but this is a simpler solution
             -- you can add a wait if there are any  bugs
            rootpart.Anchored = true -- makes player unable to move 
            -- this prevents the player from running forever after being anchored its a bug:
            wait(1) -- wait till player is anchored
            for i,track in pairs(hum:GetPlayingAnimationTracks()) do -- get all playing animations
                track:Stop() -- stops each track 
            end
        end
    end
    wait(0.2) -- wait to make it lag less 
    deb = false -- turns debounce off 
end)

--[[ PROBLEMS:
When the part height isn't right then there could be unwanted animations playing.
Also if the animations dont stop, maybe make it a local script and disable the animationscript]]
1
ok i made some small adjustments and it works perfectly. thanks man! TheBlurryKnight 10 — 3y
0
No problem! :) sayer80 457 — 3y
Ad

Answer this question