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

How do i use "for i = 1,#workspace:GetChildren() do"?

local tweenservice = game:GetService("TweenService") 
script.Parent.OnServerEvent:Connect(function(plr)
	local fireball = game.ReplicatedStorage.fireball.Part:Clone() 
	fireball.Parent = workspace 
	game.Debris:AddItem(fireball,3)
	fireball.CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 4
	tweenservice:Create(fireball,TweenInfo.new(3,0,0),{CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 350,Transparency = 1},Enum.EasingStyle.Quint,Enum.EasingDirection.Out):Play()
	local alreadytouched = false 
	local fireparticle = game.ReplicatedStorage.fireball.Par:Clone()
	fireparticle.Parent = plr.Character.Torso
	game.Debris:AddItem(fireparticle,3)
	fireball.Touched:Connect(function(hit)
		
		if alreadytouched == true then return end 
		if hit.Parent.Name == plr.Name then return end 
		if hit.Parent:FindFirstChild("Humanoid") then
			for i = 1,#workspace:GetChildren() do
				if workspace:GetChildren()[i]:IsA("Part") or workspace:GetChildren()[i]:IsA("MeshPart") then
					hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 20
					plr.Character.HumanoidRootPart.CFrame = fireball.CFrame
					fireball:Destroy()
					wait(0.5)
					fireparticle:Destroy()
				end
					for i = 1,1 do
						hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 0
						wait(1)
					end
				end
			end
		end)
	wait(2.3)
	alreadytouched = true
end)

so basically i want it so when the fireball hits any parts in workspace u teleport to the fire ball i tried using the for i ... and it doesnt work and it doesnt give a error either

1 answer

✓ Accepted answer

If you want the player to teleport to any part the the fireball touches, you simply have to set the CFrame of the HumanoidRootPart to the CFrame of the fireball. I don't understand why you're trying to loop through all the children of workspace.

Here's how you script should look like:

-- Services
local Players = game:GetService("Players")
-- Variables
local localChar = localPlayer.CharacterAdded:wait()
local localCharHumRootPart = localChar:WaitForChild("HumanoidRootPart")

whichPart.Touched:Connect(function(hit)
		if not hit:IsDescendantOf(localChar) then
			-- Teleporting the player to the CFrame of the part
			local humRootPart = localChar:FindFirstChild("HumanoidRootPart")
			humRootPart.CFrame = whichPart.CFrame
			whichPart:Destroy()
			whichPart = nil
		end
	end)

Let me know how it goes.

2 comments

0

it seems that i was retarded thank you

0

Haha, you're welcome :)

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