How to Tween Parts in a Model for a Tycoon Game

This is my first DevForum post so I apologize if I lack proper etiquette, please do correct me if there is anything I’m doing incorrectly.

I’ve been trying to find out for hours now how I would go about Tweening parts in a model from invisible to visible. I’ve managed to get the button for a tycoon to slowly fade away, but I cannot get all of the parts in the model the button is buying to slowly fade in.

This is a large portion of the script I’m using, though the issue arises when I get to the area where I create the local for tweenservice.

for i, v in pairs(Buttons:GetChildren()) do
	local NewItem = BoughtItems:FindFirstChild(v.Item.Value)
	if NewItem ~= nil then
		Items[NewItem.Name] = NewItem:Clone()
		NewItem:Destroy()
	else
		v.ButtonPart.Transparency = 1
		v.ButtonPart.CanCollide = false
		v.ButtonPart.BillboardGui.Frame.Visible = false
	end

	if v:FindFirstChild("Dependency") then
		coroutine.resume(coroutine.create(function()
			v.ButtonPart.Transparency = 1
			v.ButtonPart.CanCollide = false
			v.ButtonPart.BillboardGui.Frame.Visible = false
			if BoughtItems:WaitForChild(v.Dependency.Value, 100000) then
				v.ButtonPart.Transparency = 0
				v.ButtonPart.CanCollide = true
				v.ButtonPart.BillboardGui.Frame.Visible = true
			end
		end))
	end

	v.ButtonPart.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
			if Values.OwnerValue.Value == Player then
				if v.ButtonPart.CanCollide == true and v.ButtonPart.Transparency == 0 then
					if v:FindFirstChild("Price") and Player:WaitForChild("leaderstats").Cash.Value >= v.Price.Value then
						Player.leaderstats.Cash.Value -= v.Price.Value
						Items[v.Item.Value].Parent = BoughtItems
						v.ButtonPart.Building:Play()
						local TweenService = game:GetService("TweenService")
						local time = 2.25 --Time for tween to take
						local ButtonTween = TweenService:Create(v.ButtonPart, TweenInfo.new(time), {Transparency = 1})
						
						ButtonTween:Play()
						wait(2.25)
						
						v:Destroy()
					end
					if v:FindFirstChild("KillPrice") and Player:WaitForChild("leaderstats").Kills.Value >= v.KillPrice.Value then
						Player.leaderstats.Kills.Value = v.KillPrice.Value
						Items[v.Item.Value].Parent = BoughtItems
						v.ButtonPart.Building:Play()
						local TweenService = game:GetService("TweenService")
						local time = 2.25 --Time for tween to take
						local ButtonTween = TweenService:Create(v.ButtonPart, TweenInfo.new(time), {Transparency = 1})
						
						ButtonTween:Play()
						wait(2.25)
						
						v:Destroy()
					end
				end
			end
		end
	end)
end

Thank you for your time if you get around to helping me out here.

2 Likes

First off you could use Tween.Completed:Wait() as opposed to a wait(2.25). I’d also use local TweenService = game:GetService(“TweenService”) at the top level of the script so it’s accessible everywhere and you don’t end up calling it multiple times.

So you want to take a model and tween all of the parts of it from Transparency = 1 to Transprency = 0?

You can do something like:

local function TweenVisible(Model, Time)
    local VisibleInfo = TweenInfo.new(Time, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
    local Tweens = {}
    for _, v in pairs(Model:GetDescendants()) do
        if v:IsA("BasePart") then
            local Tween = TweenService:Create(v, VisibleInfo, {Transparency = 0})
            Tweens[v] = Tween
            Tween:Play()
        end
    end 
    return(Tweens)
end

Then simply call the function something like local Tweens = TweenVisible(Model, 5) and you can wait for any Tween[Part].Completed:Wait() to determine when it’s completed. From there you’ll probably want to destroy the tweens.

8 Likes

I get the following error:
attempt to index nil with ‘GetDescendants’

                        local Model = BoughtItems[v.Item.Value]
						
						local function TweenVisible(Model, time)
							local VisibleInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
							local Tweens = {}
							for i, v in pairs(Model:GetDescendants()) do
								if v:IsA("BasePart") then
									local Tween = TweenService:Create(v, VisibleInfo, {Transparency = 0})
									Tweens[v] = Tween
									Tween:Play()
								end
							end 
							return(Tweens)
						end

Did I incorrectly describe to the script what the variable “Model” is, or is that unrelated?

1 Like

The function itself would be separate from the loop where you’re setting up the buttons. So further up you’d have the function as it’s own separate block, and after the ButtonTween:Play() and Wait() events (assuming that’s where you want the model to tween in) you’d call the function like so:

local Tweens = TweenVisible(BoughtItems[v.Item.Value], 5) --Runs the code and stores results

This calls the function supplying it with it’s first argument of the item (gets stored in the function with the first argument variable “Model”), and 5 as an example time of how long the tweens will last (gets stored in the function as the second argument “Time”). Also make sure to change the 1 at the start of the VisibleInfo in the function to the variable Time like it is in the function definition, I forgot initially and just had it’s time parameter as one

Creating a function like that is essentially the same as a Roblox function, say FindFirstChild(Name) where the argument you pass is the name of what you want to find. In this case the function accepts the Model to tween and the Time for the tween to take.

2 Likes

I don’t know why I got so confused there, sorry, super tired right now lol. Thank you, though I think I’ll just stick with everything having the variable time so everything syncs up. What was getting me was me thinking I had to identify what “Model” and “Time” were in the lines of code you gave. I’m pretty new to coding so I definitely have a lot to more to learn regarding “i, v in pair” (or whatever the proper term is for them).

Thank you, for helping me fix that and reminding me that I should just keep the variable of TweenService at the top of the script to keep my code neat, I’ll have to check my other scripts to make sure I didn’t make that error multiple times.

2 Likes