Issue with "wait()" function in my code

I have a very strange issue, I may just be having a brain fart, but I have a veriable set to 0.1, and when I wait() this variable, the wait never ends.
Code area:
image
(please note that this is a local script)
As you can see, I have a few print checks, that are just “aaaa” but each one is longer than eachother, I even have it print the number variable before the wait.
image
In this screenshot, you can see that two of the "aaa"s print, which are all before the wait, and then the script seems to end when it’s not supposed to and I ended the test.

This has genuinely been driving me insane, please help.

1 Like

Hello,
use task.wait(), wait is considered deprecated

cheers

1 Like

Hi, sorry, but since when was this the case?
Also, this still did not work

1 Like

can you give more information as to where this script is placed and what it does? does it still happen if you move the task.wait to somewhere later in the loop?

1 Like

for some reason it worked for me maybe i’m doing it wrong?

local speed = 0.1

for i=1, 20000 do
	print("aaaaa")
	print("aaaaaaaa")
	print(speed)
	wait(speed)
	print("aaaaaaaaaa")
end
1 Like

image
The script in question is the one I have highlighted, the full code is:

local minX = -script.Parent.Size.X/2
local maxX = script.Parent.Size.X/2
local minY = script.Parent.Position.Y
local maxY = script.Parent.Position.Y
local minZ = -script.Parent.Size.Z/2
local maxZ = script.Parent.Size.Z/2

print(script.Parent.Position)
print(math.random(minX,maxX), math.random(minY, maxY), math.random(minZ, maxZ))
local speed = 0.1
local connection
for i=1, 200000 do
	print("aaaaa")
	minX = -script.Parent.Size.X/2
	maxX = script.Parent.Size.X/2
	minY = script.Parent.Position.Y
	maxY = script.Parent.Position.Y
	minZ = -script.Parent.Size.Z/2
	maxZ = script.Parent.Size.Z/2
	print("aaaaaaaa")
	print(speed)
	wait(speed)
	print("aaaaaaaaaaaa")
	local rainDrop = script.RainDrop:Clone()
	print("aaaaaaaaaaaaaaaaaaa")
	rainDrop.CFrame = CFrame.new(math.random(minX,maxX), math.random(minY, maxY), math.random(minZ, maxZ)) * CFrame.Angles(0, 0, 90)
	rainDrop.Parent = script.Parent
	rainDrop.CanCollide = false
	rainDrop.Anchored = false
	rainDrop.Touched:Connect(function()
		rainDrop:Destroy()
	end)
end

(sorry for sloppy code I hadn’t had the chance to clean it up because it doesn’t really work)
This code works perfectly fine in a server script in a part in workspace, but server scripts don’t work with the method I am trying to do and will cause problems in the future.
This is a basic script that creates realistic rain above the player’s character.

1 Like

I’m not really sure what in my code is causing this issue

1 Like

image
is this “Rain” part being parented somewhere in the workspace within the rain script? (and consequently all of its children)

this would explain why the script stops working suddenly, localscripts run in very specific contexts, and not in workspace
(so after waiting for 0.1, the main script had already moved the Rain part to workspace, causing the “LocalScript” to stop executing)

1 Like

Okay that would actually make sense, and yes I have an organized place I put this stuff in. I will try moving the contents of the LocalScript into the Rain script (as of now it just parents and moves the part according to the player’s camera), and I’ll get back to you.
image

1 Like

Please do, this behavior is definetly the culprit, though note that if you wanted to run client code in workspace you could have just changed the “RunContext” property of this script to client:
image

though this would make you run into the issue where the code will run within the startergui folder, way before its even parented to workspace (so you should probably disable it, and enable the script after cloning via code)


though note that the issues you are coming across are related to relying on execution behavior when parenting to workspace, which I don’t recommend. Client code isn’t really made to be written this way (mostly servercode more commonly does this, but the practice is still not something I would recommend)

Is there a better way to do this? I am testing it with all the game graphics disabled and I am realizing that it now isn’t updating the position variables like I have intended in the script, it’s staying at the parts original location which is at 0, 32, 0 instead of where the part is being parented over the user’s camera.

use a part with a ParticleEmitter instance with a rain texture, then move the part with the camera?

The rain is designed to not go below/through/under parts

Perhaps this is a logical error with your code since you’re dividing Size of all three coordinates by 2 in a for loop making it to atomic size and the script cannot further divide it. kindly use task.wait() instead of wait() function since it’s deprecated

task.wait() did not work, also it isn’t making an atomic number, it’s updating each variable according to the part above the players camera. (I guess I’ll still be using task.wait() anyways apparently)

I’ve fixed my code, but am coming up on another issue,
image
Anything I put before the rainDrop:Destroy() line just makes that line do absolutely nothing…
How far behind am I on LocalScript logic?

I mean the rainDrop would get destroyed on the exact same frame so it’d make sense why it seems to do nothing

No that’s not the point. Can you elaborate?

It appears that when the parts hit the FallenPartsDestroy zone, it just doesn’t remove them at all??
(It’s only reaching the zone because there’s terrain and no supporting part under the terrain, I could fix this fairly easily by adding that, but this seems to be a really strange problem.)


Edit: After a lot of thinking I’ve determined that the issue was that the parts were being cloned from another service, and with it being cloned to workspace from a service through a LocalScript, the parts were not detected as in Workspace on the server. This is a very odd issue, and I’ve never encountered this before.

You can fix this very easily by having the LocalScript create the part in workspace, instead of cloning an existing part from another service, or do the simple solution of creating a base that when the part touches it it destroys the part.

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