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

How to count studs infront of the humanoid?

Asked by 1 year ago
Edited 1 year ago

Im making a game that allows you to say a weapon and it will appear. I want to make it so a wall will appear a few studs away from the player when they say "wall". I am trying to make the wall appear a few studs in front of the HumanoidRootPart. Here is my script:

local CallMsg = "wall"

local item = game.ReplicatedStorage.Wall

game.Players.PlayerAdded:Connect(function(player) 
    player.Chatted:Connect(function(msg) 
        if msg == CallMsg then 

            local original = item
            local copy = original:Clone()
            copy.Parent = game.Workspace

            wait(3)

            copy:Destroy()
        end
    end)
end)

2 answers

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

Relative, you say? The answer is lookvector. CFrame roblox documentation

If you dont know, its part of CFrame. CFrame is a property ALL "BaseParts" have. CFrame is just physical data like Location and ROtation and stuff. The humanoidrootpart is a type basepart. So if you wanted to summon a part in front of the player 5 studs away, code would look like this.

local hrp = plr:Waitforchild"HumanoidRootPart" -- gets the humanoidrootpart
local originCFrame = hrp.CFrame -- gets the humanoidrootpart's cframe

local newCFrame = originCFrame * CFrame.new(originCFrame.LookVector *5) -- sets newCFrame to the NEW cframe which is the humanoidrootpart cframe PLUS 5 studs ahead RELATIVE to the humanoidrootpart.

[insert part you wish to summon, here].CFrame = newCFrame

I am using "* CFramew.new()" here to add it. multiplying CFrames just adds them, I dont know why its multiplication, i forgot since its not useful information to me. And FYI using only one paremeter (a Vector3 type) when making a CFrame.new() makes a new cframe thats basically empty except for location values such as X,Y, and Z. Also, the LookVector is a Vector3 which is an X,Y, and Z only. To explain a Lookvector now, All look vector is, is the RELATIVE position exactly 1 stud away in that direction of whatever part the cframe belongs to. So hrp.CFrame.Lookvector i believe gets the position thats a stud away in FRONT of humanoidrootpart. SO all you gotta do is multiply that by 5 and its 5 studs in front of humanoidrootpart. Also, the reason multiplying this by 5 works and doesnt offset everything by 5 times the normal amount and ruin it is because its a relative position MEANING the Vector3 treats the humanoidrootpart as the origin (0, 0, 0) instead of the worlds origin of (0, 0, 0), So this isnt the actual world origin, its just the RELATIVE origin here. Its not a world position at this point. So multiplying by 5 makes does multiply it all by 5 but just relative to humanoidrootpart, meaning all it does is end up on a point thats 5 studs away thats in the same direction the original lookvector is away from the humanoidrootpart origin instead of away from the world origin. And it adds that to the humanoidrootpart's cframe by translating it to world position auTomAtiCAllY.

And if you dont like cframe. here is the version without extra cframe usage, so it doesnt care about rotation or anyhting, only Position.

local hrp = plr:Waitforchild"HumanoidRootPart" -- gets the humanoidrootpart

local newPos = hrp.Position + (hrp.CFrame.LookVector *5) -- sets newPos to the NEW position which is the humanoidrootpart position PLUS 5 studs ahead RELATIVE to the humanoidrootpart.

[insert part you wish to summon, here].Position = newPos

Hope this helps.

Ad
Log in to vote
0
Answered by 1 year ago

I have found an answer, You have to refrence the humandoid root part then you have to make the part's position the same as the humanoid root part but add to the x value so it is in front like so:

local CallMsg = "wall"
local HRP = game.Workspace:FindFirstChild("HumanoidRootPart")
local item = game.ReplicatedStorage.Wall

game.Players.PlayerAdded:Connect(function(player) 
    player.Chatted:Connect(function(msg) 
        if msg == CallMsg then 

            local original = item
            local copy = original:Clone()
            copy.Parent = game.Workspace
            copy.Position = HRP.Position.X + 5

            wait(3)

            copy:Destroy()


        end
    end)
end)
0
nevermind its wrong LOL PeterParker17386 16 — 1y
0
the X is relative to the player Puppynniko 1059 — 1y
0
wdym by that? PeterParker17386 16 — 1y

Answer this question