Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
2
Answered by
mc3334 649 Moderation Voter
5 years ago

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!

0
EDIT: you also cant disable a script from itself, I suggest you just use :wait() instead mc3334 649 — 5y
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! Bgonzalezseeya11 27 — 5y
Ad
Log in to vote
0
Answered by 4 years ago

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.

Answer this question