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

How do I make a part be shot in the same direction that the player's torso is facing?

I'm trying to make a fighting game, but this script isn't working.

local player = game.Players.LocalPlayer

mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key == "f" then
		if game.Workspace:FindFirstChild("Pew " .. player.Character.Name) then
			while true do
				wait()
			end
		end
		print("Fired")
		p = Instance.new("Part", workspace)
		b = Instance.new("BodyVelocity", p)
		p.Name = ("Pew " .. player.Character.Name)
		p.Shape = ("Ball")
		p.Size = Vector3.new(2.5,2.5,2.5)
		p.Anchored = true
		p.Position = player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.lookVector * 5	
		p.CanCollide = false
		b.Velocity = p.CFrame.lookVector * 5
		p.Touched:connect(function(part)
			p:Destroy()
			local parent = part.Parent
			if game.Players:GetPlayerFromCharacter(parent) then
				parent.Humanoid.Health = parent.Humanoid.Health - 60
				wait()
			end
		end)
		
		wait(5)
		p:Destroy()
	end
end)

When the script runs, the ball doesn't move at all. Any help?

3 comments

0

You should be using UIS or CAS to retrieve key input.

0

Are you getting any errors in the output window?

0

I'm not getting any errors in the output window

1 answer

✓ Accepted answer

While I do not like the method you went about to write this, there were only a couple problems in your code:

  • Anchoring the part
  • Not setting the MaxExtents
  • Not breaking the joints (this fixes some issues with BodyVelocities and parts)

So, in effect, you just need to make sure the part is Unanchored, the max extents are set to Vector3.new(math.huge,math.huge,math.huge), and you need to break the joints of the part.

Here's the edited code:

local player = game.Players.LocalPlayer

mouse.KeyDown:connect(function(key)
    key = key:lower()
    if key == "f" then
        if game.Workspace:FindFirstChild("Pew " .. player.Character.Name) then
            while true do
                wait()
            end
        end
        print("Fired")
        local p = Instance.new("Part", workspace)
        local b = Instance.new("BodyVelocity", p)
        p.Name = ("Pew " .. player.Character.Name)
        p.Shape = ("Ball")
        p.Size = Vector3.new(2.5,2.5,2.5)
		p.Anchored = false -- EDITED
        p.Position = player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.lookVector * 5   
        p.CanCollide = false
		b.MaxForce = Vector3.new(1/0,1/0,1/0) -- EDITED // note: 1/0 == math.huge
        b.Velocity = p.CFrame.lookVector * 5
		p:BreakJoints() -- ADDED
        p.Touched:connect(function(part)
            p:Destroy()
            local parent = part.Parent
            if game.Players:GetPlayerFromCharacter(parent) then
                parent.Humanoid.Health = parent.Humanoid.Health - 60
                wait()
            end
        end)

        wait(5)
        p:Destroy()
    end
end)

Here's a much better version of the code using cleaner, more up to date mechanics:

local player		=game.Players.LocalPlayer
local userinput		=game:GetService("UserInputService")

local bulletspeed	=5
local lifetime		=5
local damage		=60
local debounce		=true

local function shoot()
	-- debounce
	debounce			=false
	
	-- calculations
	local rootpart		=player.Character:FindFirstChild("HumanoidRootPart")
	local rootcf		=rootpart and rootpart.CFrame
	local firedir		=rootcf.lookVector*bulletspeed
	
	-- rendering
	local bullet		=Instance.new("Part",workspace)
	local vel			=Instance.new("BodyVelocity",bullet)
	bullet.Size			=Vector3.new(2.5,2.5,2.5)
	bullet.Name			="Bullet"
	bullet.Shape		="Ball"
	bullet.CFrame		=rootcf*CFrame.new(0,0,-3)
	bullet.CanCollide	=false
	vel.MaxForce		=Vector3.new(1/0,1/0,1/0)
	vel.Velocity		=firedir
	bullet:BreakJoints()
	
	-- touch
	bullet.Touched:Connect(function(hit)
		local hum		=hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
		if hum then
			local hitchar	=hum.Parent
			if hitchar then
				local hitplayer	=game.Players:GetPlayerFromCharacter(hitchar)
				if hitplayer and hitplayer~=player then
					debounce=true
					bullet:Destroy()
					hum:TakeDamage(damage)
				end
			end
		end
	end)
	
	-- lifetime
	if lifetime and type(lifetime)=="number" and lifetime>0 then
		delay(lifetime,function()
			if bullet then
				bullet:Destroy()
				debounce=true
			end
		end)
	end
end

userinput.InputBegan:Connect(function(input,processed)
	if processed then return end
	local keycode	=input.KeyCode
	if keycode==Enum.KeyCode.F then
		shoot()
	end
end)

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