I don't understand why my egg hatching system

Hey there!
I’m a bit stuck on my script right now :confused:.
I’m trying to make an egg hatching system on my Roblox Game.
I successfully achieve the script to check the magnitude (distance between the egg and the player) + the UserInputService (Press E to hatch the egg). I put the script into “PlayerStarterScript”.

Now I want to make the egg hatching script.
I’m following the DevKing’s steps on this video: https://www.youtube.com/watch?v=k0S5wOVlrPs&t=792s

I replace the ClickDetector by the UserInput, but I have some problems.
The first is my script is located into the StarterPlayerGui(LocalScript) and his script is inside the clickdetector(script).

I tried to adapt his script into my InputService but I have an errormessage wich I don’t understand.

This how my script looks like now:

UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer;
local char = player.Character or player.CharacterAdded:Wait();
local HRP = char:WaitForChild("HumanoidRootPart");
local ReplicatedStorage = game:GetService("ReplicatedFirst")
local DataModule = require(game:GetService("ReplicatedStorage"):WaitForChild("DataModule"))
local Pets = ReplicatedStorage:WaitForChild("Pets")
local eggPart = workspace["Common Egg"].eggBasePart

player.CharacterAdded:Connect(function()
	HRP = player.Character:FindFirstChild("HumanoidRootPart");
end)

local function checkMagnitude(p0, p1, distance)
	-- Check is p0 is in range of distance from p1;
	return (p0.Position - p1.Position).Magnitude <= distance;
end



UserInputService.InputBegan:Connect(function(input,gameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.E then
		
		if (not checkMagnitude(HRP, eggPart, 20)) then
			-- Player is too far from the eggPart
			return; -- Terminate the rest of the script
		end
		
		if player.leaderstats.Coins.Value >= DataModule.Common.EggCost then
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - DataModule.Common.EggCost
			
		local function ChoosePet(player)
			local PlayerGui = player:WaitForChild("PlayerGui")
			local PetGui = PlayerGui.PetGui
			local EggIcon = PetGui.EggIcon
			local PetImages = PetGui.PetImages
			
			local timer = tick()
			
			EggIcon.Visible = true
			
			while tick() - timer < DataModule.Common.EggOpeningLength do
				wait(.1)
				EggIcon.Rotation = math.random(-9,9)
			end
			
			local petNumber = math.random(1,50)
			local petFound = false
			local pet = nil
			while petFound == false do
				for i, v in pairs(DataModule.Common.Pets) do
					if math.random(1,v) == petNumber then
						petFound = true
						pet = i
					end
				end
			end
			
			EggIcon.Visible = false
			
			local PetImage = PetImages:FindFirstChild(pet)
			PetImage.Visible = true
			wait(2)
			PetImage.Visible = false
			
			return pet
			end			
		end
	end
end)

I think the problem come from the localisation of my script.
I don’t have any idea to fix it. :confused:

Does anyone of you has an idea ?

Thank you for reading me! :slight_smile:

1 Like

If you haven’t noticed, you have the function ChoosePet however you haven’t actually called the function yet, hence no actions will follow from it.

1 Like

So if I’m right, I have to define the ChoosePet function out my UserInput function?

So it should be something like this:

UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer;
local char = player.Character or player.CharacterAdded:Wait();
local HRP = char:WaitForChild("HumanoidRootPart");
local ReplicatedStorage = game:GetService("ReplicatedFirst")
local DataModule = require(game:GetService("ReplicatedStorage"):WaitForChild("DataModule"))
local Pets = ReplicatedStorage:WaitForChild("Pets")
local eggPart = workspace["Common Egg"].eggBasePart
local function ChoosePet(player)
	local PlayerGui = player:WaitForChild("PlayerGui")
	local PetGui = PlayerGui.PetGui
	local EggIcon = PetGui.EggIcon
	local PetImages = PetGui.PetImages
			
	local timer = tick()
			
	EggIcon.Visible = true
			
	while tick() - timer < DataModule.Common.EggOpeningLength do
			wait(.1)
			EggIcon.Rotation = math.random(-9,9)
		end
			
	local petNumber = math.random(1,50)
	local petFound = false
	local pet = nil
	while petFound == false do
			for i, v in pairs(DataModule.Common.Pets) do
				if math.random(1,v) == petNumber then
					petFound = true
					pet = i
				end
			end
		end
			
	EggIcon.Visible = false
			
	local PetImage = PetImages:FindFirstChild(pet)
	PetImage.Visible = true
	wait(2)
	PetImage.Visible = false
			
	return pet
end

player.CharacterAdded:Connect(function()
	HRP = player.Character:FindFirstChild("HumanoidRootPart");
end)

local function checkMagnitude(p0, p1, distance)
	-- Check is p0 is in range of distance from p1;
	return (p0.Position - p1.Position).Magnitude <= distance;
end



UserInputService.InputBegan:Connect(function(input,gameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.E then
		
		if (not checkMagnitude(HRP, eggPart, 20)) then
			-- Player is too far from the eggPart
			return; -- Terminate the rest of the script
		end
		
		if player.leaderstats.Coins.Value >= DataModule.Common.EggCost then
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - DataModule.Common.EggCost
			
			ChoosePet()
		end
	end
end)

Am I right?

1 Like

To me, that looks right? Try testing it as I’m not fully sure.

1 Like

I have an errormessage.
It is:
15:02:30.769 - Image “” failed to load: Unexpected URL

15:02:36.108 - Infinite yield possible on ‘ReplicatedFirst:WaitForChild(“Pets”)’

15:02:36.109 - Stack Begin

[15:02:36.110 - Script ‘Players.Padrox_x.PlayerScripts.Press E Script’, Line 7]

15:02:36.110 - Stack End

Line 5:

Should read:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

For continuity purposes as well, line 1 should read:

local UserInputService = game:GetService("UserInputService")
1 Like

Oh nice, I don’t have anymore the errormessage when I launch the game.

But when I press E, I have an errormessage:

[15:08:02.466 - Players.Padrox_x.PlayerScripts.Press E Script:10: attempt to index nil with ‘WaitForChild’]

15:08:02.468 - Stack Begin

[15:08:02.469 - Script ‘Players.Padrox_x.PlayerScripts.Press E Script’, Line 10 - function ChoosePet]

[15:08:02.469 - Script ‘Players.Padrox_x.PlayerScripts.Press E Script’, Line 68]

15:08:02.470 - Stack End

In ChoosePet a required argument is Player, delete this required argument as its defined above:

local function ChoosePet()
	local PlayerGui = player:WaitForChild("PlayerGui")
	local PetGui = PlayerGui.PetGui
	local EggIcon = PetGui.EggIcon
	local PetImages = PetGui.PetImages
			
	local timer = tick()
			
	EggIcon.Visible = true
			
	while tick() - timer < DataModule.Common.EggOpeningLength do
			wait(.1)
			EggIcon.Rotation = math.random(-9,9)
		end
			
	local petNumber = math.random(1,50)
	local petFound = false
	local pet = nil
	while petFound == false do
			for i, v in pairs(DataModule.Common.Pets) do
				if math.random(1,v) == petNumber then
					petFound = true
					pet = i
				end
			end
		end
			
	EggIcon.Visible = false
			
	local PetImage = PetImages:FindFirstChild(pet)
	PetImage.Visible = true
	wait(2)
	PetImage.Visible = false
			
	return pet
end

Nice!
The egg appears and shakes but after the shaking delay, there is a new errormessage when the egg disapears. :confused:

[15:17:53.341 - Players.Padrox_x.PlayerScripts.Press E Script:39: attempt to index nil with ‘Visible’]

15:17:53.342 - Stack Begin

[15:17:53.343 - Script ‘Players.Padrox_x.PlayerScripts.Press E Script’, Line 39 - function ChoosePet]

[15:17:53.343 - Script ‘Players.Padrox_x.PlayerScripts.Press E Script’, Line 66]

15:17:53.343 - Stack End

The image for your pet:
image

PetImage = PetImages:FindFirstChild(pet)

Doesn’t exist in:

local PetImages = PetGui.PetImages

If the iamge doesn’t exist then its a nil object, thats why “:FindFirstChild()” returned nil.

You need to add an image label there with the corresponding image etc which can appear and disappear.

I would update this section to:

local PetImage = PetImages:FindFirstChild(pet)
if PetImage then
	PetImage.Visible = true
	wait(2)
	PetImage.Visible = false
end

In case it doesn’t work anyway.

It’s working perfeclty ! Tahnk you so much !

There is still two problems:
-The first one is that the player is able to spam the egg and to hatch multiple eggs at the same time.

  • The second one is what have I to do when the egg hatched?
    ( Where goes the pet ? and how? (By creating a clone()?))
  1. You can prevent spamming through a debounce.

  2. What you do with the pet is your decision, as the pet is returned I would most likely create a pet handling scenario with said pet and saving it. This is obviously all your choice now. And if you want to create more threads about making a pet system etc then feel free to, but obviously try it yourself first and see where you can get.

1 Like

Mhh, you’re admirable :slight_smile:

Can you become my Roblox Scripter Tutor?
I can understand if you wont for multiple reasons even because i’m probably a lost of time for you.
But I think I have so much more to learn from you as myself.

I’m unfortunately not doing tutoring right now, however I’m working on a community resource to help people with self tutoring etc. so I’d look out for that monstrosity when its done.

Its fine by the way, I do this at your pleasure as its fun and interesting - just obviously make sure to have a break once you’ve done something big like this so you don’t work yourself up :pray:

1 Like

Thank your for your help !
I’m happy to met someone like you!

I’m happy because I achieve the egg opening system + the debounce !

Now I need just quite few things to be good to go on pets, I HATE pets now! x’)

I’m working on them since 1 months (time to be accepted and to ask some help)