Tween breaks after reset

Howdy Developers,

I’m creating a pet system for my game, but there’s an issue with the tween of the egg model after the player resets.

There’s an error which states:
image

I’ve asked multiple people and tried to see if I could find the bug, but I can’t.

The error only occurs after the player dies or resets and tries to buy another egg.

Here’s the code of the script that errors:
(Sorry about the 16 lines of variables :sweat_smile:)

local part = workspace:WaitForChild("TestPart")
local replicatedStorage = game:GetService("ReplicatedStorage")
local gameClient = replicatedStorage:WaitForChild("GameClient")
local eggs = gameClient:WaitForChild("Eggs")
local petModels = gameClient:WaitForChild("Pets")
local petModule = require(gameClient:WaitForChild("Modules").PetModule)
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local eggGui = plr:WaitForChild("PlayerGui"):WaitForChild("MainGui"):WaitForChild("EggFrame")
local petGui = plr:WaitForChild("PlayerGui"):WaitForChild("MainGui").PetFrame
local sound = workspace["Egg Hatching Sound"]
local billBoardUi = plr:WaitForChild("PlayerGui"):WaitForChild("BillboardGui")
local remoteEvents = gameClient:WaitForChild("RemoteEvents")
local petViewEvent = remoteEvents:WaitForChild("PetViewEvent")
local debounce = false

local function openEgg(actionName, inputState)
	if actionName == "OpenEgg" and inputState == Enum.UserInputState.Begin then
		if (part.Position - HumanoidRootPart.Position).magnitude <= 15 then
			if debounce == false then
				debounce = true
				
				eggGui.Visible = true
				billBoardUi.Enabled = false
				
				eggGui:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), "Out", "Bounce", 1)
				wait(1)
				
				local t = 5 --// How ever manytimes to do it
				repeat wait(.1)
					eggGui.Rotation = 20
					sound:Play()
					wait(0.1)
					eggGui.Rotation = -20
					sound:Play()
					t = t -1
				until t <= 0

				eggGui.Visible = false
				eggGui.Rotation = 0
				eggGui.Position = UDim2.new(0.5, 0, -0.259, 0)
				
				local myPet = petModule.getPet(1)
								
				petViewEvent:FireServer(myPet)
				wait(2.75)
				billBoardUi.Enabled = true
				debounce = false
			end
		else
			print("no")
		end
	end
end

CAS:BindAction("OpenEgg", openEgg, false, Enum.KeyCode.E)

If you could help me out with the bug, please help me!

Thanks, Dash :smiley:

Probably because you’re attempting to start the tween again which references an object that has been de-parented by either a manual call to destory, or automatic due to the Character’s model being removed from the workspace on respawn.

Ahh, I see. I don’t really know how I would go to fix that, but I’ll try to figure it out. Thanks!

Disable this on the gui
image

2 Likes

The solution was so simple and me struggling a lot with this issue! Thank you very much! :pray:

1 Like

lol nice. Glad it helped you. :joy: