First things first, "player" in lines 35-37 is not defined anywhere. Script doesn't understand what 'player' is.
Second, "gold", "items" and "spaces" in lines 52-54 are also not defined. Even if you create a variable in function, it doesn't work outside it.
I think your problem is, that you don't understand how to use scopes. Here's a Roblox tutorial about it: https://developer.roblox.com/en-us/articles/Scope
Also, there's a nice Roblox tutorial that explains how to correctly save player's data on exit: https://developer.roblox.com/en-us/articles/Saving-Player-Data
Making it the way you've done it is a bad practice. After player leaves the game script can't access "leaderstats" object, because it's already gone. You should do this the way as it's shown in the second link :)
And also, when you test DataStore, I recommend doing it in actual game, not in Roblox Studio. DataStore in Roblox Studio doesn't always work with game.Players.PlayerRemoving.
Hope I helped :)