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 the ball follow the character's torso but 2 studs in front of it?

So, I need the ball to stay anchored but update the position and always stay 2 studs in front of the character's torso, but I'm confused as to how to do that. Help me please?

player = game.Players.LocalPlayer
char = game.Players.LocalPlayer.Character
hum = game.Players.LocalPlayer.Character.Humanoid
mouse = game.Players.LocalPlayer:GetMouse()
 
mouse.KeyDown:Connect(function(key)
key = key:lower()
if key == "e" then
local fb = game:GetService("ServerStorage").Fireball:Clone()
game:GetService("Chat"):Chat(char.Head, "Fireball!", "Red")
fb.Parent = game.Workspace
fb.Anchored = true
if char.UpperTorso and char.LowerTorso then fb.CFrame = char.UpperTorso.CFrame + Vector3.new(0, 0, -2)
        else
    fb.CFrame = char.Torso.CFrame + Vector3.new(0, 0, -2)
end
end
end)

1 answer

✓ Accepted answer

You should probably use something like head or rootpart if you use R15. Regardless you need a simple loop that does multiple times what you already scripted. I will also clean up your syntax and make some suggestions.

--you should refer to the variables you define
player = game.Players.LocalPlayer
char = player.Character or player.CharacterAdded:wait()
hum = char:WaitForChild('Humanoid')
head = char:WaitForChild('Head')
mouse = player:GetMouse()
uis = game:GetService('UserInputService') --mouse KeyDown is deprecated.
keys = Enum.KeyCode

uis.InputBegan:connect(function(input,gpe)
	if not gpe then --not on an interface
		local key = input.KeyCode
		if key == keys.E then
			local fb = game:GetService("ServerStorage").Fireball:Clone()
			game:GetService("Chat"):Chat(head, "Fireball!", "Red")
			fb.Parent = game.Workspace
			fb.Anchored = true
			while wait() do
				fb.CFrame = head.CFrame + Vector3.new(0,0,-2)
			end
		end
	end
end)

player.CharacterAdded:connect(function(character)
	--make sure these variables are updated
	char = character
	hum = char:WaitForChild('Humanoid')
	head = char:WaitForChild('Head')
end)

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