Chicken model laying egg

Hi,

I’m still fairly beginner to Lua scripting, but I have a question. I modelled, rigged and animated a chicken, but I want an egg to spawn near it on the same spot every 1 minute. The player can pick it up with proximity prompt, and it adds it to leaderstat as “egg”

What’s the best and easy way of doing this?

I want them to spawn like this.

Screenshot 2022-10-04 at 19.32.44

4 Likes

here it says how to spawn items in a radius.

for spawning around the chicken:
local position = chicken.Position + Vector3.new(X,Y,Z)

1 Like

What about cloning, but it’s on the same position, which is behind the chicken.

2 Likes
local clone = egg:Clone()   --clones the egg
clone.Parent = workspace    --sets the parent to workspace
clone.Position = position   -- sets the clone agg position
2 Likes

I have this, but it’s not quite working.

Egg = game.ServerStorage.Egg
proximityprompt = Egg.ProximityPrompt
eggsspawned = 0

studsbehindchicken = 2

while wait(10) do
	if eggsspawned < 3 then
		local newEgg = Egg:Clone()
		newEgg.CFrame = script.Parent.CFrame+(script.Parent.CFrame.LookVector*-studsbehindchicken)
		newEgg.Parent = workspace
		eggsspawned+=1
		newEgg.ProximityPrompt.Triggered:Connect(function(plr)
			print('clicked')
			local leaderstats = plr.leaderstats
			local Eggs = leaderstats.Eggs

			Eggs.Value+=1
			eggsspawned-=1
			newEgg:Destroy()
		end)
	end
end
2 Likes
newEgg.Position = (chicken.Position + chicken.CFrame.LookVector * -eggsspawned)

LookVector is a Vector3

2 Likes

I have this now, but the proximity prompt of the egg disappears after a few seconds when the egg is not picked up.

Egg = game.ServerStorage.Egg
proximityprompt = Egg.ProximityPrompt
eggsspawned = 0

studsbehindchicken = 2

while wait(10) do
	if eggsspawned < 3 then
		local newEgg = Egg:Clone()
		newEgg.CFrame = script.Parent.CFrame+(script.Parent.CFrame.LookVector*-studsbehindchicken)
		newEgg.Parent = workspace
		eggsspawned+=1
		newEgg.ProximityPrompt.Triggered:Connect(function(plr)
			spawn(function()
				local leaderstats = plr.leaderstats
				local Eggs = leaderstats.Eggs

				Eggs.Value+=1
				eggsspawned-=1
			end)
			newEgg:Destroy()
		end)
		delay(10,function()
			newEgg.ProximityPrompt:Destroy()
		end)
	end
end
1 Like
delay(10,function()
	newEgg.ProximityPrompt:Destroy()
end)

why do you add that

1 Like

Idk, the prompt always dissapears. How do I prevent that?

1 Like

Delete that function entirely as it deletes the proximity prompt after 10 seconds. If you want to delete the egg after 10 seconds, change it to newEgg:Destroy().

2 Likes

It’s still not working. It still removes the prompt for some reason

Egg = game.ServerStorage.Egg
proximityprompt = Egg.ProximityPrompt
eggsspawned = 0

studsbehindchicken = 2

while wait(10) do
	if eggsspawned < 3 then
		local newEgg = Egg:Clone()
		newEgg.CFrame = script.Parent.CFrame+(script.Parent.CFrame.LookVector*-studsbehindchicken)
		newEgg.Parent = workspace
		eggsspawned+=1
		newEgg.ProximityPrompt.Triggered:Connect(function(plr)
			spawn(function()
				local leaderstats = plr.leaderstats
				local Eggs = leaderstats.Eggs

				Eggs.Value+=1
				eggsspawned-=1
			end)
			newEgg:Destroy()
		end)
	end
end
1 Like

My only assumption is interference with other scripts. There’s nothing in that script that would delete the proximity prompt, unless your changes haven’t been saved.

1 Like

There’s nothing interfering. I think I just need a better script, probably. I want the egg to spawn behind the chicken and pile the eggs up by three max.

Bumping this hoping some can help :slight_smile:

Bumping again. Is there really no one that can help?

Hello. I will write up some new code for you. Do you want 3 eggs to spawn at a time? Or just one spawning in one of the random 3 positions?

Egg = game.ServerStorage.Egg
proximityprompt = Egg.ProximityPrompt
eggsspawned = 0

studsbehindchicken = 2
max_eggs = 3 -- please keep lower than (or equal to) 9 :)

while wait(10) do
	if eggsspawned < max_eggs then
		local newEgg = Egg:Clone()
		local c = script.Parent.CFrame 
		eggsspawned += 1
		-- there's a better way of doing this but oh well
		newEgg.CFrame = c* (CFrame.Angles(0, math.rad((7+eggsspawned)*360 / 9), 0)) * CFrame.new(0, 0, studsbehindchicken)
		newEgg.Parent = workspace
		if (not newEgg:FindFirstChild('ProximityPrompt')) then
			local px = Instance.new('ProximityPrompt')
			px.Parent = newEgg
		end
		newEgg.ProximityPrompt.Triggered:Connect(function(plyr)
			if plyr and plyr:FindFirstChild('leaderstats') and plyr.leaderstats:FindFirstChild('Eggs') then
				plyr.leaderstats.Eggs.Value += 1
			end
			eggsspawned-=1
			newEgg:Destroy()
		end)
	end
end

One in one of the tree random positions. The chicken can only have up to three eggs and then it stops spawning until the rest are picked up by the player :slight_smile:

Remove the line that have that keep destroying (Meaning to remove.) the part.

(Yes, I’m sorry to bump this topic.)