File not creating when it should

Godot Version

4.2.2

Question

When the folder needs creation, I’m trying to make a file with .txt and it gives error code 7. Also, the folder creation gives an error code 31.

Here’s the code:

func Counter_folder():
var dir = DirAccess.open(“user://”)
if !dir:
var error = dir.get_open_error()
print("Failed to open user directory: ", error)
return

var counter = DirAccess.open("user://Counter")
if counter == null:
	var result = dir.make_dir("user://Counter")
	if result:
		print("Created Counter folder.")
		# Initialize counter file
		var counter_file
		counter_file = FileAccess.open("user://Counter/counter.txt", FileAccess.WRITE)
		if !counter_file:
			var error = FileAccess.get_open_error()
			print("Failed to create counter file: ", error)
	elif !result:
		var error = DirAccess.get_open_error()
		print("Failed to create Counter folder: ", error)

func save_world():
var saved_worlds = list_saved_worlds()

if saved_worlds.size() >= 10:
	print("Maximum number of saved worlds reached. Cannot save more.")
	return

var counter = FileAccess.open("user://Counter/counter.txt", FileAccess.READ_WRITE)
if !counter:
	print("Error opening counter file: ", FileAccess.get_open_error())
	return


var world_number = counter.get_line().to_int()
world_number += 1
counter.seek(0)
counter.store_line(str(world_number))
counter.close()

var file_path = "user://Saves/world_" + str(world_number) + ".MW"
print("Saving world to file:", file_path)


var file
file = FileAccess.open(file_path, FileAccess.WRITE)
if !file:
	var error = FileAccess.get_open_error()
	print("Error opening file for saving ", str(error))
	return



for block in get_children():
	if block is StaticBody3D:
		file.store_var(block.transform.origin)

file.close()
print("World saved successfully at ", file_path)

Try using error_string(err) for a more useful message

print("Failed to create Counter folder: ", error_string(error))

it gives me invalid parameter

Could you paste the script between three ticks like so

```
type or paste code here
```

And point out which lines give which errors? Like with a comment in the paste?

ticks are on the `~ key, you can use the forum’s </> button or ctrl+e to create the three ticks for you

‘’’
extends Node3D

var terrain_width = 100
var terrain_length = 100
var terrain_height = 4
var block_scene = preload(“res://blocks/Dirt/dirt.tscn”)

func _ready():
generate_terrain()
randomize()
Saves_folder()
Counter_folder()
save_world()

Generate terrain procedurally

func generate_terrain():
for x in range(terrain_width):
for z in range(terrain_length):
var height = randi() % terrain_height
for y in range(height):
var block_instance = block_scene.instantiate()
block_instance.transform.origin = Vector3(x, y, z)
add_child(block_instance)

func _on_save_time_timeout():
save_world()

func _on_button_pressed():
save_world()
get_tree().change_scene_to_file(“res://Menu/menu.tscn”)

func Saves_folder():
var dir = DirAccess.open(“user://”)
if !dir:
var error = dir.get_open_error()
print("Failed to open user directory: ", error)
return

var save_folder = DirAccess.open("user://Saves")
if save_folder == null:
	var result = dir.make_dir("user://Saves")
	if result == OK:
		print("Created Saves folder.")
	else:
		var error = DirAccess.get_open_error()
		print("Failed to create Saves folder: ", error)

func Counter_folder():
var dir = DirAccess.open(“user://”)
if !dir:
var error = dir.get_open_error()
print("Failed to open user directory: ", error)
return

var counter = DirAccess.open("user://Counter")
if counter == null:
	var result = dir.make_dir("user://Counter")
	if result:
		print("Created Counter folder.")
		# Initialize counter file
		var counter_file
		counter_file = FileAccess.open("user://Counter/counter.txt", FileAccess.WRITE)
		if !counter_file:
			var error = FileAccess.get_open_error()
			print("Failed to create counter file: ", error)
	elif !result:
		var error = DirAccess.get_open_error()
		print("Failed to create Counter folder: ", error)

func save_world():
var saved_worlds = list_saved_worlds()

if saved_worlds.size() >= 10:
	print("Maximum number of saved worlds reached. Cannot save more.")
	return

var counter = FileAccess.open("user://Counter/counter.txt",

FileAccess.READ_WRITE)
if !counter:
print("Error opening counter file: ", FileAccess.get_open_error())
return

var world_number = counter.get_line().to_int()
world_number += 1
counter.seek(0)
counter.store_line(str(world_number))
counter.close()

var file_path = "user://Saves/world_" + str(world_number) + ".MW"
print("Saving world to file:", file_path)


var file
file = FileAccess.open(file_path, FileAccess.WRITE)
if !file:
	var error = FileAccess.get_open_error()
	print("Error opening file for saving ", str(error))
	return



for block in get_children():
	if block is StaticBody3D:
		file.store_var(block.transform.origin)

file.close()
print("World saved successfully at ", file_path)

Load the world from a file

func load_world(file_path):
var file = FileAccess.open(file_path, FileAccess.READ)
if !file:
print(“Error opening file for loading.”)
return

for child in get_children():
	if child is MeshInstance3D:
		child.queue_free()

while file.get_position() < file.get_length():
	var position = file.get_var()
	var block_instance = block_scene.instantiate()
	block_instance.transform.origin = position
	add_child(block_instance)

file.close()
print("World loaded successfully from " + file_path)

List saved worlds in the directory

func list_saved_worlds():
var dir = DirAccess.open(“user://Saves”)
var saves =
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != “”:
if file_name.ends_with(“.MW”):
saves.append(file_name)
file_name = dir.get_next()
dir.list_dir_end()
return saves
‘’’
var counter = FileAccess.open(“user://Counter/counter.txt”,
FileAccess.READ_WRITE)
if !counter:
print("Error opening counter file: ", FileAccess.get_open_error())
return
and
var counter = DirAccess.open(“user://Counter”)
if counter == null:
var result = dir.make_dir(“user://Counter”)
if result:
print(“Created Counter folder.”)
# Initialize counter file
var counter_file
counter_file = FileAccess.open(“user://Counter/counter.txt”, FileAccess.WRITE)
if !counter_file:
var error = FileAccess.get_open_error()
print("Failed to create counter file: ", error)
elif !result:
var error = DirAccess.get_open_error()
print("Failed to create Counter folder: ", error)

Y forgot that when it gives error 31 it creates folder and the error