Im trying to make a tree fall over when the tree reaches value 5 with tweening but i dont know how to use tween service, and whenever i attack the tree it creates a new amount value.
local Players = game:GetService("Players")
local goi = Players.LocalPlayer.PlayerGui.Gui
local Tween = game:GetService("TweenService")
rotation = CFrame.Angles(0,0, math.rad(90))
db = true
handle.Touched:Connect(function(hit)
if hit.Parent.Name == "Tree"and db == true then
local amount = Instance.new("IntValue")
amount.Parent = goi
local tree = hit.Parent
db = false
local modelCFrame = tree:GetPivot()
goi.CanvasGroup.Visible = true
amount.Value += 1
if amount.Value == 1 then
goi.CanvasGroup.Amount.Size = UDim2.new(0.20,0,0,75)
end
if amount.Value == 2 then
goi.CanvasGroup.Amount.Size = UDim2.new(0.4,0,0,75)
end
if amount.Value == 3 then
goi.CanvasGroup.Amount.Size = UDim2.new(0.6,0,0,75)
end
if amount.Value == 4 then
goi.CanvasGroup.Amount.Size = UDim2.new(0.8,0,0,75)
end
if amount.Value == 5 then
goi.CanvasGroup.Amount.Size = UDim2.new(1,0,0,75)
tree:PivotTo(modelCFrame * rotation)
amount:Destroy()
goi.CanvasGroup.Visible = false
goi.CanvasGroup.Amount.Size = UDim2.new(0.2,0,0,75)
end
task.wait(1)
db = true
else
print(hit.Parent.Name)
end
end)
You’ll need to define a Tween that changes the rotation of the tree gradually over time. To prevent creating a new IntValue
every time you attack the tree, you should first check if the IntValue
already exists in the PlayerGui
.
Try something like this? I would also recommend reading up on TweenService itself, since I can only infer what you want to accomplish with it.
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local goi = Players.LocalPlayer.PlayerGui.Gui
local rotation = CFrame.Angles(0, 0, math.rad(90))
local db = true
handle.Touched:Connect(function(hit)
if hit.Parent.Name == "Tree" and db == true then
local amount = goi:FindFirstChild("Amount")
if not amount then
amount = Instance.new("IntValue")
amount.Name = "Amount"
amount.Parent = goi
end
local tree = hit.Parent
db = false
local modelCFrame = tree:GetPivot()
goi.CanvasGroup.Visible = true
amount.Value = (amount.Value or 0) + 1
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local goal = {CFrame = modelCFrame * rotation}
local tween = TweenService:Create(tree, tweenInfo, goal)
if amount.Value == 1 then
goi.CanvasGroup.Amount.Size = UDim2.new(0.20, 0, 0, 75)
elseif amount.Value == 2 then
goi.CanvasGroup.Amount.Size = UDim2.new(0.4, 0, 0, 75)
elseif amount.Value == 3 then
goi.CanvasGroup.Amount.Size = UDim2.new(0.6, 0, 0, 75)
elseif amount.Value == 4 then
goi.CanvasGroup.Amount.Size = UDim2.new(0.8, 0, 0, 75)
elseif amount.Value == 5 then
goi.CanvasGroup.Amount.Size = UDim2.new(1, 0, 0, 75)
tween:Play()
amount:Destroy()
goi.CanvasGroup.Visible = false
goi.CanvasGroup.Amount.Size = UDim2.new(0.2, 0, 0, 75)
end
task.wait(1)
db = true
else
print(hit.Parent.Name)
end
end)
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local goi = Players.LocalPlayer.PlayerGui.Gui
local rotation = CFrame.Angles(0, 0, math.rad(90)) -- Desired end rotation
local db = true
handle.Touched:Connect(function(hit)
if hit.Parent.Name == "Tree" and db then
local tree = hit.Parent
local amount = tree:FindFirstChild("Amount") or Instance.new("IntValue", tree)
amount.Name = "Amount"
db = false
goi.CanvasGroup.Visible = true
amount.Value += 1
-- Update GUI based on amount value
local newSizeScale = amount.Value * 0.2
goi.CanvasGroup.Amount.Size = UDim2.new(newSizeScale, 0, 0, 75)
if amount.Value == 5 then
-- Tween the tree falling over
local modelCFrame = tree:GetPivot()
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- Easing style
Enum.EasingDirection.Out, -- Easing direction
0, -- Repeat count (0 = no repeat)
false, -- Reverses (true/false)
0 -- Delay time
)
local goal = {CFrame = modelCFrame * rotation}
local tween = TweenService:Create(tree, tweenInfo, goal)
tween:Play()
tween.Completed:Connect(function()
amount:Destroy()
goi.CanvasGroup.Visible = false
goi.CanvasGroup.Amount.Size = UDim2.new(0.2, 0, 0, 75)
end)
end
task.wait(1)
db = true
else
print(hit.Parent.Name)
end
end)
You could also just maybe