Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script fails the first time, but the second and after it works like it should?

TO BE MORE PRECISE:I believe the problem is at line 5, where it clones it prematurely from the ServerStorage rather than moving the chick to the egg. I have an egg script that will hatch an NPC when something gets near it, however the first time it spawns where the NPC's location is at (in ServerStorage) and after the script has re-enabled itself, the script works like how I intended it. Detail: I touch the egg, it plays the audio and spawns the NPC far away. When I go back and try it again, the NPC spawns on the egg (like I intended). Code:

function touch(hit)
	local chick = game.ServerStorage:WaitForChild('chick')
	local open = script.Parent.Open --the sound
	local rootPart = script.Parent --the egg
	local chickBaby = chick:Clone() --maybe why it spawns away from the egg?
	wait(2)
	open:Play()
	wait(3)
	script.Disabled = true
	chickBaby:Clone()
	chickBaby.Parent = workspace
	chick:SetPrimaryPartCFrame(rootPart.CFrame)
	wait(20)
	script.Disabled = false
end	


script.Parent.Touched:connect(touch)

2 answers

✓ Accepted answer

I believe the issue is because you arent checking if hit is a player, so when it spawns and touches the ground, the function runs and disables itself prematurley. To fix this, add in between lines 1 and 2:

if not hit.Parent:FindFirstChild("Humanoid") then return end

that should prevent the code from firing when touching non players!

2 comments

0

EDIT: you also cant disable a script from itself, I suggest you just use :wait() instead

0

The egg is anchored and the script does not fire when I enter the game. I made sure the egg is floating slightly so this would not happen, and the "script.Disabled" is a debounce. However, the code is useful so thank you!

I figured it out by playing with the script. Apparently local chickBaby is not needed and can function fine with just chick. That is why the chick would spawn somewhere else instead of the egg.

This discussion is preserved as it was. New answers, comments, and votes are closed.