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

I need help with CFraming a Part's Y Position?

Asked by 6 years ago

So, I'm trying to make a script that when the Player presses "e', It creats a Part and the Part goes to antoher part called "Buidling". My problem is that I need the Part to go to a certain Y Postion depending on the Player's Torso's Y Postion. So like if the Player is at a certain height, the Part will be a higher than the Player's height. But I need it so that the Part's Y Position will never pass the Building's height. Any idea how to do that?

Thank's in advance (And sorry if I sound confusing... it's really hard to explain.)

1 answer

Log in to vote
0
Answered by
kazeks123 195
6 years ago
Edited 6 years ago

You should thank me a lot since I basically wrote the script for you:

uis = game:GetService("UserInputService") --Getting input service
player = game.Players.LocalPlayer --Getting player
part = nil --Variable for your part
buildingHeight = 20 --Your building height (building's top Y coordinate)
building = game.Workspace.building --Your building


uis.InputBegan:connect(function(input) --Getting player's inputs
    if input.KeyCode == Enum.KeyCode.E then --Checking if E key was pressed
        part = Instance.new("Part") --Creating new part
        part.Anchored = true
        part.CanCollide = false --Optional property if you don't want player to be pushed by part
        part.Parent = game.Workspace --Getting part to workspace
        part.CFrame = player.Character.Torso.CFrame * CFrame.new(0,3,0) --Teleporting part 3 studs above player's torso
        part.Orientation = Vector3.new(0,0,0) --Resetting part's rotation
        if part.Position.Y > buildingHeight then --Checking if part is not above building height
            local difference = buildingHeight - part.Position.Y --Getting difference from heights
            part.CFrame = part.CFrame * CFrame.new(0,difference,0) --Subtracting difference from part's overall height
        end
        part.Position = Vector3.new(building.Position.X,part.Position.Y,building.Position.Z) --Teleporting part to building
    end
end)

This will get part in the building's center (add offset to the Vector3 in line 20 if you don't want the part to be in the building brick) 3 studs above player's torso's position or at the top of building if player is above it. It MUST be placed in a LocalScript to work, which has to be placed in the player's character's model (or StarterPack / StarterGui).

0
Thank you MRbraveDragon 374 — 6y
Ad

Answer this question