Hey guys, I have this tool that when activated spawns a part(plane) that you can control with a lookVector. Now in principle it works ok, but I wanted to add a BodyPosition to the part so that it moves towards the player when it spawns. (or spawns near the player)
Now the code I use to add BodyVelocity to the part works ok and is created in the part, but the same code for BodyPosition doesn’t create a BodyPosition in the part, and it does not produce a syntax error.
What am I doing wrong?
Am I missing something here?
Maybe there is another way to spawn the part near the player when the tool is activated. Are you able to help?
repeat wait() until game.Players.LocalPlayer.Character
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local tool = script.Parent
local plane = script.Parent.Plane
local enabled = false
tool.Activated:Connect(function()
if enabled == false then
enabled = true
local planeVelocity = Instance.new("BodyVelocity", plane)
planeVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
local planePosition = Instance.new("BodyPosition", plane)
planePosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
planePosition.Parent = plane
planePosition.Position = player.Character.HumanoidRootPart.Position + Vector3.new(2, 2, 2)
while enabled == true do
planeVelocity.Velocity = mouse.Hit.lookVector * 20
wait()
end
end
if enabled == true then
enabled = false
plane:FindFirstChild("BodyVelocity"):Destroy()
plane:FindFirstChild("BodyPosition"):Destroy()
end
end)
Thanking you in advance. All suggestions are appreciated.
I might be misunderstanding, but aren’t you moving the plane with planeVelocity on a loop? BodyPosition is not very strong and does not grantee that the part will reach the set position, in my tests i noticed that it never reaches the exact body position even without interference. Try disabling the velocity temporarily and see if the plane will move towards the set position
Oh wow I never thought of that. Without, the BodyVelocity enabled it actually came to the player as i was expecting. So maybe I need to finish summoning the part before I create the BodyVelocity instance.
Unless there is a better way to summon this part to the players position.
You can anchor it and tween it to the desired position then just unanchor it, that way velocity will not interfere, you can also just spawn the part right next to the player
Hmmm interesting, I’m pretty much a noob at this but the tween option seems more manageable for me. Because I know about cloning but this part is within a tool and I will clone the tool from inventory, but not sure how I would then clone the part within the tool. Unless spawning the part is a different thing.
Anyway you’ve given me plenty of thing to research. Thank-you.
Doesn’t matter where a part is. All you need to is
local Clone = Tool.Part:Clone()
Clone.Parent = workspace
Clone.Name = “ClonedPart”
now it’s a completely different part. You can clone it from the tool but you might as well keep it in Replicated Storage if you intend to use it as a template for cloning
If you feel your problem is solved be sure to mark the solution so that others can see it if they have a similar issue and are looking for a solution