Saving problem

Hi!

I am currently making a saving script for buying items in-game and for some reason the data saves and loads normally but when I restart the game the data saved in that script returns back to a previous version of itself. Could you help me with this issue?

This is my script(its a gui_script):
Thank you!
-Catherine

    my_file_path = sys.get_save_file("zombies", "shop")
    local data2 = sys.load(my_file_path)
shop = true

function init(self)
    msg.post(".", "acquire_input_focus")
    


pprint(data2)

if data2.supplies == nil then
data2.supplies = 3453

end

if data2.basicpistol == nil then
data2.basicpistol =true

	
print(data.basicpistol)
end

 if data2.sawedoff == nil then
data2.sawedoff = false

end   


prices = {sawedoff = 25}


w = data2.supplies
if not sys.save(my_file_path,data2) then
	print("o no")
	end
end

function final(self)
    -- Add finalization code here
    -- Remove this function if not needed
end

function update(self, dt)
	
w = data2.supplies
label=	gui.get_node("text2")
gui.set_text(label, w)
    
end

function on_message(self, message_id, message, sender)
    -- Add message-handling code here
    -- Remove this function if not needed
end

function on_input(self, action_id, action)

local options = gui.get_node("Options")
local bpistol = gui.get_node("basicpistol")
local sawedoff = gui.get_node("sawedoff")
			
			
		if gui.pick_node(options, action.x, action.y) and action.pressed then
        	
        	gui.set_scale(options,vmath.vector3(0.95,0.95,1))
			for i = 1,8 do
			local c = gui.get_node("coin"..i.."b")
				gui.set_color(c,vmath.vector4(1,0,0,1))
			end
			if shop == false then
			shop = true
		gui.set_color(gui.get_node("box3"),vmath.vector4(0,0,0,1))
			
		else
		shop = false
		gui.set_color(gui.get_node("box3"),vmath.vector4(0,0,0,0))
		end
		elseif gui.pick_node(options, action.x, action.y) and action.released then
			gui.set_scale(options,vmath.vector3(1,1,1))
			for i = 1,8 do
			local c = gui.get_node("coin"..i.."b")
				gui.set_color(c,vmath.vector4(1,1,1,1))
			end
		
		
		
		elseif gui.pick_node(bpistol,action.x,action.y) and action.pressed and shop ==true then
		gui.set_scale(bpistol,vmath.vector3(1.95,1.95,1))
		
		if data2.basicpistol == true then
		for i = 1,8 do
			local c = gui.get_node("coin"..i.."")
				gui.set_color(c,vmath.vector4(0,1,0,1))
			end
		--DÉSACTIVER LES AUTRES
			for i = 1,8 do
			local c = gui.get_node("coin"..i.."c")
				gui.set_color(c,vmath.vector4(1,1,1,1))
			end
			
			-----
		else
			for i = 1,8 do
			local c = gui.get_node("coin"..i.."")
				gui.set_color(c,vmath.vector4(1,0,0,1))
			end
		
		end
		elseif gui.pick_node(bpistol,action.x,action.y) and action.released and shop ==true then
		gui.set_scale(bpistol,vmath.vector3(2,2,1))
		pprint(data2)
			if data2.basicpistol == true then
		for i = 1,8 do
			local c = gui.get_node("coin"..i.."")
				gui.set_color(c,vmath.vector4(0,1,0,1))
			end
		--DÉSACTIVER LES AUTRES
			for i = 1,8 do
			local c = gui.get_node("coin"..i.."c")
				gui.set_color(c,vmath.vector4(1,1,1,1))
			end
			
			-----
		msg.post("/char#script", "changegun", {gun = "basicpistol"})
		
		
		else
			for i = 1,8 do
			local c = gui.get_node("coin"..i.."")
				gui.set_color(c,vmath.vector4(1,1,1,1))
			end
		
		end
		
		
		elseif gui.pick_node(sawedoff,action.x,action.y) and action.pressed and shop ==true then
		gui.set_scale(sawedoff,vmath.vector3(1.95,1.95,1))
		
		if data2.sawedoff == true then
		print(data2.supplies)
		for i = 1,8 do
			local c = gui.get_node("coin"..i.."c")
				gui.set_color(c,vmath.vector4(0,1,0,1))
			end
		
		
		elseif data2.sawedoff == false and data2.supplies-prices.sawedoff >= 0 then
		for i = 1,8 do
			local c = gui.get_node("coin"..i.."c")
				gui.set_color(c,vmath.vector4(0,1,0,1))
			end
			data2.sawedoff = true
			print(data2.supplies)
			data2.supplies = data2.supplies-prices.sawedoff
			print(data2.supplies)
			if not sys.save(my_file_path,data2) then
	print("o no")
	end
	print(data2.supplies)

If you have not yet seen DefSave it will make this easy for you https://www.defold.com/community/projects/87727/

2 Likes

Thanks! I’ll try it soon:D

The lack of proper indentation in the code snippet you shared makes it super hard to read! Try to not mix spaces with tabs and try to be consistent in your indentation style