First off, you are setting the pencil's position to some random place, not in front of the player. I am going to assume this is a local script and help you with that. Yes, you can control cloned parts position
First you need to get the player's character and torso. You can access the player with local game.Players.LocalPlayer(local script only). You get character with Player:FindFirstChild("Character"), and torso with Character:FindFirstChild("Torso"). You then need the CFrame of the torso, or Torso.CFrame. to get ahead of the player instead of inside, you should use LookVector, LookVector gets the position a ahead, behind, or if you get fancy, to the sides of a player. we can use this by doing part.Position = Torso.CFrame.LookVector*5 (the times 5 parts is the number of studs ahead. For behind, you use divide). It should look something like this so far. Also, declare the shootingPencil clone as a variable or it will just make 2 more clones.
local uis = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local char = player:WaitForChild("Character")
local torso = char:FindFirstChild("Torso")
uis.InputBegan:connect(function(input)
if input.InputType == ""
if input.KeyCode == Enum.KeyCode.Space then
local pencil = game.ServerStorage.shootingPencil:Clone()
pencil.Parent = workspace
pencil.Position = torso.CFrame.LookVector * 5
end
end)
Now for the shooting up part, for that we will use BodyVelocity. BodyVelocity make an object move to a certain position at a certain speed. We will use it to make the pencil shoot up. First create a new BodyVelocity with Instance.new() then set it's velocity to go up, the y value, or second value, makes it go up. Then set it's MaxForce to infinity with math.huge. Your script should look like this:
Lastly, you need to eventually get rid of the pencil, so you should use the Debris service in Roblox. When an item is put into debris, after a specified time, it disappears. you can get debris with game:GetService("Debris"). Down below is finished code.
local uis = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local char = player:FindFirstChild("Character")
local torso = char:FindFirstChild("Torso")
local debris = game:GetService("Debris")
uis.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
local pencil = game.ServerStorage.shootingPencil:Clone()
pencil.Parent = workspace
pencil.Position = torso.CFrame.LookVector * 5
local vel = Instance.new("BodyVelocity", pencil)
vel.Velocity = Vector3.new(0,100,0)
vel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
debris:AddItem(pencil, 10)
end
end)
1 comment
Umm... there is no space between Enum.KeyCode.Space and then on line 6, don't know if thats the problem tho