Issues with TweenService

I created a script for my tycoon system which will tween a model up from the ground, the object the player is purchasing by stepping on the button will be moved from the tycoons temporary storage in ReplicatedStorage to workspace and the position will be reduced by 1 stud on the y-axis and tweened upwards by 1 stud to give it the effect of being built, the only issue is that when reducing the position of the model it lowers the position by around 10 studs instead of 1, I have no idea why this is happening and have tried so much to fix this.

local tycoon = script.Parent.Parent
local tycoonTemp = game.ServerStorage.TycoonTemp:FindFirstChild(tycoon.Name)
local buttons = tycoon.Buttons

local tweenService = game:GetService("TweenService")

for i, button in pairs(buttons:GetChildren()) do
	button.Trigger.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local object = button.Object.Value
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			if button.Price.Value <= player.leaderstats.Cash.Value then
				for _, buttonPart in pairs(button:GetChildren()) do
					if buttonPart:IsA("BasePart") then
						local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
						local propertiesToChange = {Transparency = 1, Position = buttonPart.Position + Vector3.new(0, -0.3, 0)}
						local buttonFade = tweenService:Create(buttonPart, tweenInfo, propertiesToChange)	
						buttonFade:Play()
					end
				end
				button.Trigger.Details.Enabled = false
				object.Parent = tycoon.Purchases

-- tweening starts here

				for i, part in pairs(object:GetDescendants()) do
					if part:IsA("BasePart") then
						part.Position = part.Position - Vector3.new(0, 1, 0)
						part.Transparency = 1
						local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
						local propertiesToChange = {Transparency = 0, Position = part.Position + Vector3.new(0, 1, 0)}
						local modelFadeIn = tweenService:Create(part, tweenInfo, propertiesToChange)
						modelFadeIn:Play()
					end
				end
				object.Purchased.Value = true
			else
				print("Cannot afford")
			end
		end
	end)
end

When a player touch the button, you did this statement:
if game.Players:GetPlayerFromCharacter(hit.Parent) then

The legs, the feet, HumanoidRootPart, Head and any part in the character are valid for that statement. Example: Head.Parent is the Character, same as other parts.
You are triggering all the code inside that statement multiple times, including the Tweens. I guess theres a chance that is causing weird behaviour with the Tween, and will cause very weird behaviour when trying to reduce money, and other stuff related with the button.

The button should be triggered once, not multiple times from multiple collisions with character’s parts.

How did I not notice this :person_facepalming:, I’ll fix this by setting CanTouch to false, thanks for the reply!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.