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

Press space to shoot a brick up, my script isn't working!?

When space is pressed, I want a MeshPart of mine, to shoot up into the air, a couple studs infront of the player.

Here what I have


local uis = game:GetService('UserInputService')


uis.InputBegan:connect(function(input,process)
    if not process then
        if input.KeyCode == Enum.KeyCode.Spacethen
           --[[ local test = Instance.new("Part",workspace)
			test.Position = Vector3.new(158, 868.169, 12) ]]--
			game.ServerStorage.shootingPencil:Clone()
			game.ServerStorage.shootingPencil:Clone().Parent = workspace
			game.ServerStorage.shootingPencil:Clone().Position = Vector3.new(-352.5, 860.769, -5.5)
			
			
        end
    end
end)

Im not sure if you can control location of clones, or even clone meshparts

1 comment

0

Umm... there is no space between Enum.KeyCode.Space and then on line 6, don't know if thats the problem tho

1 answer

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) --second argument of Instance.new sets the parent
		vel.Velocity = Vector3.new(0,100,0)
		vel.MaxForce = Vector3.new(math.huge, math.huge, math.huge) --keeps it at a steady rate
	debris:AddItem(pencil, 10)--10 is the lifetime of the object
	end
	end)

3 comments

0

As much as this as helped me, the script isn't spawning the pencil in at all. Is it because of veriables? Its in a local script in starter pack

0

I have noticed the variables dont work, when i remove debris torso and char they work.

0

try to use lighting instead of serverstorage

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