local name = script.Name

gun=game:GetService("DataStoreService"):GetDataStore(name)

function SaveGun(player)
	gun:SetAsync(player.userId, player.gunloadout.GunName.equippedGun.Value)
	print("Owned guns saved!")
end

function LoadGun(player)
	local Gunny = gun:getAsync(player.userId)
	
	if gun ~= nil then
		player.gunloadout.GunName.equippedGun.Value = Gunny -- Error happens here, it does not take "gunloadout" as a valid member

	end
end

function onPlayerEntered(newPlayer)
		
		if newPlayer:FindFirstChild("GunsSavingData") == nil then
			local folder = Instance.new("Folder")
			folder.Name = "GunsSavingData"
			folder.Parent = newPlayer
		end

		gunloadout = newPlayer:WaitForChild("GunsSavingData") -- Gunloadout
		
		local GunName = Instance.new("Folder", gunloadout)
		GunName.Name = name
		
		local equippedGun = Instance.new("IntValue", gunloadout)
		equippedGun.Name = "HasValue"
		equippedGun.Value = "1"
		
		local children = script:GetChildren()
		
		for i = 1,#children do
			local c = children[i]:clone()
			c.Parent = GunName
		end

		GunName.Parent = gunloadout
		equippedGun.Parent = GunName
		gunloadout.Parent = newPlayer
		
		newPlayer:WaitForDataReady() 

		LoadGun(newPlayer)
end 

function onPlayerRemoving(player)
	print("Attempting to save score for " .. player.Name)
	SaveGun(player)
end

8 comments

0

are you sure that gunloadout's parent is the player?

0

In local server test, it shows as it is https://gyazo.com/d5472c982666bb058a30e83294ac5984

0

Can I see the whole player tree?

0

It should be player.GunsSavingData OOF

0

It made an error bcs you defined gunsloadout after you used it....

0

Oh man, now I feel stupid. Thanks for helping me!

0

add [SOLVED] in the title