Egg Collecting Mechanic | Attempt to index nil with Play

Hey there,

I’ve recently been creating an egg hunt collecting mechanic like the popular community-made game: Egg Hunt 2022.

You can search videos to see how that animation works. I have been trying to best to recreate it as specific as possible. I also make mistakes and this is one I can’t seem to get fixed.

Watch the video below to see the mechanic and error:

The error:
attempt to index nil with 'Play'
screenshot:

This error has been occuring since I have added a task.wait(1.5) to my script. More on that in just a second.

The script works perfectly fine without the task.wait in the Client script in the SetEgg function, but once I add it in, it glitches out into this error.

The main goal is that the egg in the viewportframe keeps spinning till the gui disappears.

Here is the mechanic structure:
image
image

Here is the Server script:

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Greenwich = require(ServerStorage:WaitForChild("Greenwich"))

local function SaveData(Player)
	local Eggs = Player.Eggs
	local Values = {
		Eggs.LavaEgg.Value;
		Eggs.VineEgg.Value;
	}
	pcall(function()
		Greenwich:GetDB("Values"):Set(Player.UserId, Values, false)
	end)
end

--Players
Players.PlayerAdded:Connect(function(Player)
	
	local Eggs = Instance.new("Folder")
	Eggs.Name = "Eggs"
	Eggs.Parent = Player
	
	local Values = Instance.new("Folder")
	Values.Name = "Values"
	Values.Parent = Player
	
	local Debounce = Instance.new("BoolValue")
	Debounce.Name = "Debounce"
	Debounce.Parent = Values
	Debounce.Value = false
	
	local LavaEgg = Instance.new("BoolValue")
	LavaEgg.Name = "LavaEgg"
	LavaEgg.Parent = Eggs
	
	local VineEgg = Instance.new("BoolValue")
	VineEgg.Name = "VineEgg"
	VineEgg.Parent = Eggs
	
	local Data

	pcall(function()
		Data = Greenwich:GetDB("Values"):Get(Player.UserId)
	end)
	
	if Data and Data[1] then
		LavaEgg.Value = false--Data[1]
	else
		LavaEgg.Value = false
	end
	
	if Data and Data[2] then
		VineEgg.Value = false--Data[2]
	else
		VineEgg.Value = false
	end
	
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character.Humanoid
		Humanoid.UseJumpPower = true
	end)
end)

Players.PlayerRemoving:Connect(function(Player)
	Greenwich:GetDB("Values"):Save(Player.UserId)
end)

--Eggs touching
for _, v in pairs(workspace.Eggs:GetChildren()) do
	v.Touched:Connect(function(Object)
		if Players:GetPlayerFromCharacter(Object.Parent) then
			local Player = Players:GetPlayerFromCharacter(Object.Parent)
			local Debounce = Player.Values.Debounce.Value
			if Debounce == false then
				Debounce = true
				if not Player.Eggs[v.Name.."Egg"].Value then
					ReplicatedStorage.RemoteEvents.Handler:FireClient(Player, "NewEgg", v)
					Player.Eggs[v.Name.."Egg"].Value = true
					Debounce = false
				elseif Player.Eggs[v.Name.."Egg"].Value then
					ReplicatedStorage.RemoteEvents.Handler:FireClient(Player, "AlreadyEgg", v)
					Debounce = false
				end
			end
		end		
	end)
end

Here is the Client script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Player = Players.LocalPlayer
local RemoteEvents = ReplicatedStorage.RemoteEvents

local EggFrame = script.Parent.EggFrame
local ViewportFrame = EggFrame.ViewportFrame
local EggLabel = EggFrame.EggLabel
local EggCollected = script.Parent.EggCollected
local CoverFrame = script.Parent.CoverFrame

local TweenInformation, fovInfo = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local Tween
local outcodeEgg 

local function replayTween(info)
	info:Play()
	--info.Completed:Wait()
--	if info.Completed then
--		info:Play()
--	end
end

local function SetEgg(Egg, fovCamera, playerHumanoid)
	local ClonedEgg = ReplicatedStorage.Eggs[Egg.Name]:Clone()
	ViewportFrame:ClearAllChildren()
	ClonedEgg.Parent = ViewportFrame
	EggLabel.Text = Egg.Name.." Egg"
	
	local Camera = Instance.new("Camera")
	Camera.Parent = ViewportFrame
	ViewportFrame.CurrentCamera = Camera
	Camera.CFrame = ClonedEgg.CFrame * CFrame.new(0, 0, Egg.Size.z * 1.5)
	
	local fovTweenEnter, fovTweenLeave = TweenService:Create(fovCamera, fovInfo, {FieldOfView = 30}), TweenService:Create(fovCamera, fovInfo, {FieldOfView = 70})
	
	outcodeEgg = ClonedEgg
	Tween = TweenService:Create(ClonedEgg, TweenInformation, {Orientation = Vector3.new(0, 360, 0)})
	fovTweenEnter:Play()
	
	CoverFrame.Visible = true
	EggCollected.Visible = true
	
	playerHumanoid.WalkSpeed = 0
	playerHumanoid.JumpPower = 0
	
	task.wait(1.5)
	EggCollected:TweenSizeAndPosition(UDim2.new(0,400, 0,100), UDim2.new(0.5,0, 0.1,0))
	task.wait(0.5)
	
	EggFrame.Visible = true
	
	replayTween(Tween)
	
	playerHumanoid.WalkSpeed = 16
	playerHumanoid.JumpPower = 50
	
	EggFrame.Visible = false
	CoverFrame.Visible = false
	EggCollected.Visible = false
	EggCollected.Position = UDim2.new(0.5,0, 0.5,0)
	EggCollected.Size = UDim2.new(0,900, 0,200)
	
	fovTweenLeave:Play()
end

RemoteEvents.Handler.OnClientEvent:Connect(function(Argument, Egg)
	if not Argument then return end
	if Argument == "NewEgg" then
		if not Player.Character.Humanoid.UseJumpPower then Player.Character.Humanoid.UseJumpPower = true end
		Egg:Destroy()
		outcodeEgg = nil
		Tween = nil
		SetEgg(Egg, workspace.CurrentCamera, Player.Character.Humanoid)
		task.wait(0.1)
		replayTween(Tween)
	elseif Argument == "AlreadyEgg" then
		Egg:Destroy()
		outcodeEgg = nil
		Tween = nil
	end
end)

while true do 
	if outcodeEgg then 
		replayTween(Tween)
	end 
	task.wait() 
end

It’s a super complicated error on my end, and I hope if any of you guys know more about it.

Thanks a lot! Enjoy your day. :smiley:

1 Like

Try:
Printing Tween below and above the line EggCollected:TweenSizeAndPosition(UDim2.new(0,400, 0,100), UDim2.new(0.5,0, 0.1,0))

print("Tween Before:", Tween)
EggCollected:TweenSizeAndPosition(UDim2.new(0,400, 0,100), UDim2.new(0.5,0, 0.1,0))
print("Tween After:", Tween)

And also put a print below the line where Tween gets set to nil like so:

RemoteEvents.Handler.OnClientEvent:Connect(function(Argument, Egg)
	if not Argument then return end
	if Argument == "NewEgg" then
		if not Player.Character.Humanoid.UseJumpPower then Player.Character.Humanoid.UseJumpPower = true end
		Egg:Destroy()
		outcodeEgg = nil
		Tween = nil
        print("Tween Set to Nil 1")
		SetEgg(Egg, workspace.CurrentCamera, Player.Character.Humanoid)
		task.wait(0.1)
		replayTween(Tween)
	elseif Argument == "AlreadyEgg" then
		Egg:Destroy()
		outcodeEgg = nil
		Tween = nil
        print("Tween Set to Nil 2")
	end
end)

Test what you were doing and check the output
It’s possible that Tween was set to nil in the RemoteEvent connect before the code did replayTween(Tween) in function SetEgg

2 Likes

I have implemented your code and recorded a fragment.

I’m a little confused on my end but I hope you know more about it. If you need my file, hit me up.

Keep in mind that Tween is only for the egg rotation and nothing else :slight_smile:

1 Like

A simple quick fix would be to do this:

local fovTweenEnter, fovTweenLeave = TweenService:Create(fovCamera, fovInfo, {FieldOfView = 30}), TweenService:Create(fovCamera, fovInfo, {FieldOfView = 70})
	
	outcodeEgg = ClonedEgg
    -- I removed a line from here
	fovTweenEnter:Play()
	
	CoverFrame.Visible = true
	EggCollected.Visible = true
	
	playerHumanoid.WalkSpeed = 0
	playerHumanoid.JumpPower = 0
	
	task.wait(1.5)
	EggCollected:TweenSizeAndPosition(UDim2.new(0,400, 0,100), UDim2.new(0.5,0, 0.1,0))
	task.wait(0.5)
	
	EggFrame.Visible = true
    -- I moved the line to here
	Tween = TweenService:Create(ClonedEgg, TweenInformation, {Orientation = Vector3.new(0, 360, 0)})
	replayTween(Tween)
	
	playerHumanoid.WalkSpeed = 16
	playerHumanoid.JumpPower = 50

Basically I moved the Tween = TweenService:Create( to right above the replayTween(Tween).
Let me know if this fixes your error

2 Likes

Thank you so much! I realize my error now :slight_smile:

Enjoy your day.

Here’s the working result:

1 Like