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

Why does my for loop instantly start without my function being activated?

local IceCube = script.Parent
	
local function IceInYaFace()
	wait(1)
	for NumberOfIce = 1, 50 do

	local IceBlocks = Instance.new("Part", game.Workspace)
	IceBlocks.Anchored = false
	IceBlocks.BrickColor = BrickColor.new("Pastel Blue")
	IceBlocks.Material = Enum.Material.Ice
	IceBlocks.Size = Vector3.new(2, 1, 1.25)
	IceBlocks.Position = Vector3.new(86, 15, 48.625)
		end
end

IceCube.Touched:Connect(IceInYaFace())

So I wanted to make a function that creates 50 ice blocks 1 sec after the script.Parent/IceCube gets touched but when I play it just spawns 50 ice blocks after 1 second even though I haven't touched the script's parent. I would appreciate an explanation of the error and how to fix.

3 answers

✓ Accepted answer

It seems the problem here is that you aren't telling the script what your Ice Block needs to be touched by before running the function. Right now, the script will run the function whenever the IceBlock is touched by anything. I am assuming you want to make the script activate only when a Character touches it. To fix this simply make the touch script identify what is being touched.

IceCube = script.Parent

IceCube.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		 wait(1)
	   	 for NumberOfIce = 1, 50 do
		    local IceBlocks = Instance.new("Part", game.Workspace)
		    IceBlocks.Anchored = false
		    IceBlocks.BrickColor = BrickColor.new("Pastel Blue")
		    IceBlocks.Material = Enum.Material.Ice
		    IceBlocks.Size = Vector3.new(2, 1, 1.25)
		    IceBlocks.Position = Vector3.new(86, 15, 48.625)
	        end
	end
end)

As you can see, here the script attempts to find a Humanoid when it is touched, and if it does, it continues with its function.

If this solves your question, please upvote, if not please clarify further.

Another BasePart might be touching it. You'd need to do a few if statements to make sure it's a player using GetPlayerFromCharacter(hit.Parent) and checking if it ~= nil. Good luck!

forgot to say that its floating and anchored so it doesnt touch anything. Will it then still work saying that if a player touches it?

1 comment

0

It may fix the problem, I dont know what your iceblock looks like or it's surroundings. It is very possible that this change fixes your problem.

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