How To make a Script wait for a Part to Appear in a folder before Performing the Rest of the script? (Touched Based)

I’m attempting to Create a Script for the Player to pick up Balls that’s spawn around the map, and throw them at players, Apon the collision Of the ball and the player, the Player gets flung in the opposite direction with extreme force.

Issue is, I’ve move my Ball Projectile Script to ServerScriptServices and It detects if the balls thrown by a RemoteEvent From the LocalScript in StarterCharacterScripts Firing it.
Once Detected by the Main Script it summons the projectile Ball Inside the a Folder called “ExistingBalls” In the Workspace with BodyVelocity in the Direction the Player is facing.

But the Main issue is that The Flinging Script Breaks due to before the Balls thrown, it cant Find the Ball in the Folder Therefore Breaking itself.

I’ve tried many different ways to fix this issue, but recently just came back to scripting after a few months of break, so i’m a bit rusty

What i’m trying to achieve is for the Flingscript to Wait until it Finds the Ball in the Folder in the workspace
Before Doing in the rest of its code, aswell as having it do this multiple times. (within testing the ball could only fling a player once before never flinging them again after Respawning the player)

If anyone has any advice, it’ll be greatly appreciated

-FlingingScript-

local debounce = false

--The Main Projectile Ball
local Balltest = workspace.ExistingBalls:WaitForChild("CometBall"):WaitForChild("HitBox")
--If Ball is Touched
	Balltest.Touched:Connect(function(hit)
	print("touched")
	local Hit_Character = hit.Parent:FindFirstChild("Humanoid").Parent
	if debounce == false then debounce = true
		--All the Variables
		local bv = Instance.new("BodyVelocity")
		local offset = Vector3.new()
		local IsRagdoll = (hit.Parent:WaitForChild("IsRagdoll"))
		local ClonedImpact = game.ReplicatedStorage.Impact.Part:Clone()
		local Particle1 = ClonedImpact.Debris
		local Particle2 = ClonedImpact.Flash
		local Particle3 = ClonedImpact.Flash2
		local Particle4 = ClonedImpact.Shards
		local Particle5 = ClonedImpact.Spinthings
		local sound1 = ClonedImpact.Sound1
		local sound2 = ClonedImpact.Sound2
		local sound3 = ClonedImpact.Sound3
		local sound4 = ClonedImpact.Sound4
		local sounds = {sound1, sound2, sound3, sound4}
		
		--Special Effects and Sounds
		
		sounds[math.random(1, #sounds)]:Play()
		ClonedImpact.Parent = workspace
		ClonedImpact.CFrame = hit.Parent:WaitForChild("Torso").CFrame * CFrame.new(0,0,0)
		Particle1:Emit(16)
		Particle2:Emit(10)
		Particle3:Emit(10)
		Particle4:Emit(16)
		Particle5:Emit(10)
		
		--The Flinging Code + Ragdoll Activation
		
		bv.Parent = (Hit_Character.Torso)
		bv.MaxForce = Vector3.new(100000,100000,100000) -- can mess with this or the 80's
		bv.Velocity = (Hit_Character.Head.CFrame.LookVector * -400)
			+ (Hit_Character.Head.CFrame.UpVector * 100)
		IsRagdoll.Value = true
		
		--Destroying the BodyVelocity of the Player + Setting HP to 0
		
		wait(0.01) bv:Destroy()
		debounce = false
		wait(3)
		hit.Parent:WaitForChild("Humanoid").Health = 0
		ClonedImpact:Destroy()
	end	wait(1)
end)

If you need more Detail, let me know

Have you tried looking into ChildAdded function?

Use :WaitForChild(). The first argument of :WaitForChild() is the part that you want to wait for.

Wait for child will halt the whole script until the child is found. If it is not found, the entire script will just keep waiting for it. There is no maximum wait time.

Example:
local folder = workspace:WaitForChild("BallFolder")

u can look for the ball in the folder or wait for it to be added in the folder

local ball = folder:FindFirstChild("Ball") or folder.ChildAdded:Wait() --considering that only ball is added in the folder
1 Like

Thanks! this Has worked but ive now run into a second issue, The Script tracks the Projectile Ball once thrown but i made it so the projectile ball would disappear (basically just Ball:Destroy()) after a few seconds, but the script still attempts to track the same exact ball Even once destroyed

I now need to figure out a way to have the script To restart itself to find another Ball once the Ball gets destroyed.

(I have spent 30-20 minutes trying to figure this out but i am still struggling-)

u can just assign it to another to continue after it gets destroyed

ball.Destroying:Once(function()
    ball = folder:FindFirstChild("Ball") or folder.ChildAdded:Wait() 
end)

Hm, this hasnt worked. I did some earlier testing with print in the script which whenever the ball gets touched it prints in the output, odd thing is apon when the ball hits for the first time. it says “Touched” in the output

but once thrown again and it hits something again, It says nothing in the output so im wondering if its the Script halfway through breaking something?

Ignore that statement, i found a fix, its quite lazy too but eh. if it works it works|

Just cloned the Flingscript from another part it was attached too in another folder then Parented it to the Projectile Ball itself. worked like a charm

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.