Need help with proximityPrompt.Triggered

Sup, developers! i wasted 5 hours already trying to understand whats going wrong with my script so here it is:

for _, rock in claimedRocksFolder:GetChildren() do
	local proximityPrompt = rock:FindFirstChild("ProximityPrompt")
	if proximityPrompt then
		proximityPrompt.Triggered:Connect(function(player)
			print(1)
			local rockName = rock.Name
			local ownerId = tonumber(rockName:match("%d+"))
			local playerId = player.UserId

			local character = player.Character
			local humanoid = character:WaitForChild("Humanoid")
			local root = character:WaitForChild("HumanoidRootPart")

			if tonumber(ownerId) == tonumber(playerId) then
				if not carrying then
					onCarry(rock, humanoid, root)
				else
					unCarry(rock, humanoid)
				end
			end
		end)
	end
end

I already know how to fix that(probably not the best idea) but if i will write
wait() right after for _, rock in claimedRocksFolder:GetChildren() do everything will work great but without wait() Triggered just wont work. I wanna ask you maybe you know some tips that can help me or some ways that i can use to finish this code. i dont really wanna to use wait() cause ig it might just broke sometime on some laggy servers. btw its a server script in a serverScriptService. I already tried to use remote events, remote functions, already used different ways like claimedRocksFolder.ChildAdded:Connect(function() end) and etc(even local script) so i got no idea more what i can do. my best guess is that adding wait() causes a short delay, which gives enough time for all the components to set up correctly(dont know maybe it will help)

4 Likes

have you tried putting wait() or game:GetService("RunService").Stepped:Wait() before the for loop begins?

4 Likes

Does using a WaitForChild for the claimedRocksFolder work before the for _ loop?
Basically checking to make sure the folder is loaded before you start trying to gain access to it.
That way if you have someone with a laggy server it won’t crash the script.

Also, don’t use wait() anymore, it’s been replaced by task.wait() for better performance.

5 Likes

Just realized that the problem was because of proximity prompt. I used module script that was recreating proximity prompt so game was setting proximityPrompt to an old one not a new one. By the way i changed script to claimedRocksFolder.ChildAdded:Connect(function(rock) because its works really better than for _, rock in claimedRocksFolder:GetChildren() do cause its not updating. Thanks everyone for help solving the problem!

5 Likes