How exactly would I make a part infront of me?

Hello everyone I don’t exactly know how I would do this. A little bit of help would be REALLY nice, lets get into the problem…
Well what I want to accomplish is that lets say whatever direction you are facing, lets say I was to press e and a part came out of it going straight. How would I do this? (Just the direction of the part though to go straight out infront of me) sorry I don’t make much sense. If you want an example, like in Dungeon Quest if you’ve ever used the Energy Orbs how it goes in front of you and goes just straight. How would I do that? Thank you for reading/potentially helping if you do!

1 Like

For creating the part you could use:

plr = Player.HumanoidRootPart--change
local Part = Instance.new("Part",Workspace)
Part.CanCollide = False
Part.CFrame = CFrame.new(plr.CFrame.X,plr.CFrame.Y,plr.CFrame.Z)---I know this line is wrong and will need to be changed, just not in studio atm
--^^ Similar But Rotate Part
--Then insert Some sort of velocity

I assume most of this is wrong but it may be a starting point.

Alright I will go play around with it, try and see if this helps me a bit, thanks!

Uh so once you press E, new part will be added to workspace. It’s position will be set based on character’s position and it’ll be facing the character. I anchored it just so you can see how it works.

local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

userInputService.InputBegan:Connect(function(input, gpe)
    if gpe then return end
    if input.KeyCode == Enum.KeyCode.E then
        local part = Instance.new("Part")
        part.Position = humanoidRootPart.Position + humanoidRootPart.CFrame.LookVector
		part.CFrame = CFrame.new(part.Position, humanoidRootPart.Position)
		part.Anchored = true
		part.CanCollide = false
        part.Parent = workspace
    end
end)
2 Likes

Thank you this is going to help A LOT

@Wizard101fire90 Please, dont do this:

local Part = Instance.new("Part",Workspace) -- can cause lag in certain cases like spawning in

also:

local plr = PlayerService .HumanoidRootPart--change to local since its quicker

just change it to a local

HOWEVER, I’m just explaining the pet peeves I have about some coding, I don’t recommend this.

Spawning it on the client will only allow one person to see it, if you want multiple to see it like in Dungeon Quest while registering hits reliabley, you’ll require remote events, (Methods to use to move it: TweenService on both client and Server (TweenServiceV2 Can be used, its a module you can find on devforums), bodyforce, bodyposition, etc.), local scripts, and server scripts.)

@rokec123, your method also works but only on client, which doesn’t allow other players to see it.

First, you would want to create a remote event preferablly in replicatedstorage as to access both client and server.

local RepStorage = game:GetService("ReplicatedStorage") -- grabs repliatedstorage
local SpawnPartRem = RepStorage:WaitForChild("SpawnPartRemoteEvent") -- get name of remote event

Now we also want to get the player to call this remote event with a key press like so:

local RepStorage = game:GetService("ReplicatedStorage") -- grabs repliatedstorage
local SpawnPartRem = RepStorage:WaitForChild("SpawnPartRemoteEvent") -- get name of remote event

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input, GPE)
    if GPE then return end -- prevents activaiting during chat
    if Input.KeyCode == Enum.KeyCode.E then
        SpawnPartRem:FireServer() -- you can add some checks though it may not work so well since it son client and can be bypassed
    end
end)

Now that we’ve called it on the client, have a script on the ServerScriptService receieve this call like so:

local RepStorage = game:GetService("ReplicatedStorage") -- grabs repliatedstorage
local Debris = game:GetService("Debris") -- to remove after a while
local SpawnPartRem = RepStorage:WaitForChild("SpawnPartRemoteEvent") -- get name of remote event

SpawnPartRem.OnServerEvent:Connect(LocalPlayer)
    local Character = LocalPlayer.Character -- there is other better methods that work more reliabley, however, just rushing
    local HRP = Character:WaitForChild("HumanoidRootPart")

    local Fireball = Instance.new("Part")
    local BodyForce = Instance.new("BodyForce") -- You can use body force or tweens on cleint + server (more knowledge needed)
    Fireball.CanCollide = false -- you can also change anything with the fireball before spawning it in

    -- Credit to rockec123 for this part, I'm too lazy myself to remember this XD
    Fireball.Position = HRP.Position + HRP.CFrame.LookVector
    Fireball.CFrame = CFrame.new(Fireball.Position, HRP.Positin
    --
    BodyForce.Force = Vector3.new() -- change however you'd like for it to move or not
    BodyForce.Parent = Fireball

    Fireball.Touched:Connect(function(Hit)
        -- Do something if you'd like when it gets touched
    end)

    Debris:AddItem(Fireball, 1) -- Fireball is part being removed, 1 is delay (you can change it)
    Fireball.Parent = workspace -- will spawn it in on workspace after everything is finished
end)

And thats how you’d get a call from the client to the server and spawn a part allowing other players to see, touch, and get hurt by it.

Magnitude checks and other checks will make it better it making it more difficult to exploit, anyways, good luck developing :+1:

2 Likes

Yeah I only wanted to give him a short introduction about how it works but thanks for expanding the topic :+1:

1 Like

Thanks! Will help a lot for what I am doing!