Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a part appear in front of a Player?

I want to make a tool that summons a wall in front of the player. I already tried a script but it did not work, there was also no errors in the output.

here is my script

local tool = script.Parent
local bool = game.StarterPack.Build.Value
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local torso = player.Character.UpperTorso

--EQUIP--

tool.Equipped:connect(function()
	bool.Value = true
end)

tool.Unequipped:connect(function()
	bool.Value = false
end)

--WALL--

mouse.KeyDown:connect(function(key)
    if key == "e" and bool.Value ==true then
        print("Pressed e")
        Wall = Instance.new("Part",game.Workspace)
        Wall.Position = torso.Position + torso.Position + torso.CFrame.lookVector*5
    end
end)


1 comment

0

It’s probably not working because you’re trying to parent a part to workspace by localscript, which won’t work. It might work if you parent it to the camera but that will only let the player with the tool see it.

3 answers

✓ Accepted answer

.CFrame = torso.CFrame.lookVector*5 + torso.Position

2 comments

0

thanks a ton!

0

Note that you shouldn't even be using torso.Position. You should be using HumanoidRootPart.Position. Still works the same, but HumanoidRootPart's position never fluctuates (even when jumping) With animations, the uppertorso will bounce around which will make the position inaccurate. Just a tip.

KeyDown is deprecated. Don't use it. Instead do this.

local context = game:GetService("ContextActionService")
local tool = script.Parent
local plr = game:GetService("Players").LocalPlayer
local char = plr.CharacterAdded:Wait()
local bool = tool.Bool

tool.Equipped:Connect(function(mouse) -- Use Connect not connect
    bool.Value = true
end)

tool.Unequipped:Connect(function() 
    bool.Value = false
end)

-- Wall function

function makeWall()
    if bool.Value == true then
        local W = Instance.new("Part",game.Workspace")
        W.Name = plr.Name.."'s Wall"
        W.CFrame = char.UpperTorso.CFrame + Vector3.new(1,0,0) 
    end
end

context:BindAction("MakeWall",makeWall,false,Enum.KeyCode.E)

3 comments

0

Thanks so much!

0

I tried your script it worked fine but it does not spawn in front of the players face?

0

scroll down to see fully

So you guys helped me make the script but like why didnt it spawn in front of the players face?

local context = game:GetService("ContextActionService")
local tool = script.Parent
local plr = game:GetService("Players").LocalPlayer
local char = plr.CharacterAdded:Wait()
local bool = tool.Value
local torso = game.Players.LocalPlayer.Character.UpperTorso


tool.Equipped:Connect(function(mouse) -- Use Connect not connect
    bool.Value = true
end)
tool.Unequipped:Connect(function()
    bool.Value = false
end)
-- Wall function
function makeWall()
    if bool.Value == true then
        local W = Instance.new("Part",game.Workspace)
        W.Name = plr.Name.."'s Wall"
        W.Size = Vector3.new (13, 13, 2)
        W.Anchored = true
        W.CFrame = char.UpperTorso.CFrame + char.UpperTorso.CFrame.lookVector*5 + torso.Position
    end
end


context:BindAction("MakeWall",makeWall,false,Enum.KeyCode.E)


that was the code I used (thanks to you guys) but it does not spawn in front of the player, it like spawns on his left or right, and sometimes behind.

This discussion is preserved as it was. New answers, comments, and votes are closed.