I’ve been trying to script a tree to fall away from the player when the player cuts it down but I couldn’t succeed in it. The script down below gives me the correct direction but the impulse doesnt push the tree part away, it just stands there as if there was no impulse. Can someone help?
local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
local Tree = script.Parent
local function invertDifference(pos1,pos2)
local diff = pos1-pos2
return pos1+diff
end
local function breakTree()
local TreeHP = Tree:GetAttribute("Health")
print(TreeHP)
if TreeHP == 0 then
Tree.Name = "CutTree"
Tree:SetAttribute("Health", nil)
local playerName = Tree:FindFirstChild("Owner").Value
local player = workspace:FindFirstChild(playerName.Name).HumanoidRootPart
for i, v in Tree.Parent:GetChildren() do
if v == Tree then
local push = invertDifference(Tree.Position, player.Position)
local newpart = Instance.new("Part")
newpart.Parent = workspace
newpart.Position = push
v:ApplyImpulse(push * 10)
end
v.Anchored = false
print(v)
end
end
end
Tree.AttributeChanged:Connect(breakTree)
Turns out i forgot to put the .Anchored = false before applying the impulse LOL
edit 1 - now the tree was falling but it always fell towards the opposite of 0,0,0 so i asked the roblox assistant, it misunderstood me and gave me a code that makes the tree fall towards me. Surprisingly it was working so i modified it to fall to the opposite side of the character. I dont know what was wrong honestly. Here’s the code:
local TreeHP = Tree:GetAttribute("Health")
print(TreeHP)
if TreeHP == 0 then
Tree.Name = "CutTree"
Tree:SetAttribute("Health", nil)
local playerName = Tree:FindFirstChild("Owner").Value
local player = workspace:FindFirstChild(playerName.Name).HumanoidRootPart
local TreeModel = Tree.Parent
for i, v in Tree.Parent:GetChildren() do
if v == Tree then
v.Anchored = false
local push = player.Position - Tree.Position
local impulsedirection = Instance.new("Part")
impulsedirection.Parent = workspace
impulsedirection.CanCollide = false
impulsedirection.Anchored = true
impulsedirection.Position = Tree.Position + push
local TreeCoord = Vector3.new(Tree.Position.X, Tree.Position.Y + 4, Tree.Position.Z)
local actualpush = push * Vector3.new(-1,1,-1)
v:ApplyImpulseAtPosition(actualpush * 60, TreeCoord)
local impulsepos = Instance.new("Part")
impulsepos.Parent = workspace
impulsepos.CanCollide = false
impulsepos.Anchored = true
impulsepos.Position = TreeCoord
end
v.Anchored = false
print(v)
end
end
end```