Why am I getting this error?

Here is the script:

local data = game:GetService('DataStoreService')
local storage = game:GetService('ServerStorage')

function savePlayerData(player)
	
	local saveData = data:GetDataStore('saveData', player.UserId)
	saveData:setAsync('money', player.leaderstats.Gold.Value + player.Refund.Value)
	
	if player.Base then
		
		-- save the old bases location
		local location = player.Base.Value:GetPrimaryPartCFrame()
		player.Base.Value:Destroy()
		
		-- create a new base by cloning from serverstorage
		if player.Base.Value == game.Workspace.Base then
			print('ye')
		elseif player.Base.Value == game.Workspace.Base2 then
			print('br')
		elseif player.Base.Value == game.Workspace.Base3 then
			print('ello')
		elseif player.Base.Value == game.Workspace.Base4 then
			print('duzi')
		end
	
	end
	
end
game.Players.PlayerRemoving:Connect(savePlayerData)

Here is the error:

error10

And here is the explorer window:
expl3


Base2 sure looks like a valid member of workspace to me :eyes:…

Any help is appreciated, and if you need more information just ask. Thanks!

Try calling WaitForChild()? :thinking: :eyes:

local data = game:GetService('DataStoreService')
local storage = game:GetService('ServerStorage')

function savePlayerData(player)
	
	local saveData = data:GetDataStore('saveData', player.UserId)
	saveData:setAsync('money', player.leaderstats.Gold.Value + player.Refund.Value)
	
	if player.Base then
		
		-- save the old bases location
		local location = player.Base.Value:GetPrimaryPartCFrame()
		player.Base.Value:Destroy()
		
		-- create a new base by cloning from serverstorage
		if player.Base.Value == workspace:WaitForChild("Base") then
			print('ye')
		elseif player.Base.Value == workspace:WaitForChild("Base2") then
			print('br')
		elseif player.Base.Value == workspace:WaitForChild("Base3") then
			print('ello')
		elseif player.Base.Value == workspace:WaitForChild("Base4") then
			print('duzi')
		end
	
	end
	
end
game.Players.PlayerRemoving:Connect(savePlayerData)

The issue is that you’re Destroy()ing the base before making the evaluations.

local location = player.Base.Value:GetPrimaryPartCFrame()
	player.Base.Value:Destroy() --Destroyed player's Base.
		
	if player.Base.Value == game.Workspace.Base then --Errors because Base has been destroyed.
	...
1 Like

Although that would be the case, that’s not line 18

Line 18 would be

So we know that player.Base.Value works for the first conditional check, otherwise I believe it’d just result in a “Attempt to index nil with Value” error if that was that case

I was taking a brief excerpt as an example. In the original post, that Base2 was the problem, but because it’s a switch it’s going to evaluate everything sequentially. The code will error once it hits the first base it can’t find, and the issue is because it’s attempting to index the now non-existent base from the workspace via the dot operator. The player.Base.Value part should always work.

1 Like

Using WaitForChild() in a ServerScript is unnecessary, as the script will only run once everything is loaded in.

You are destroying the player’s base before you start looking for it. You could change your script to look something like this instead:

local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")

local function SavePlayerData(player)
    local saveData = DataStoreService("saveData", player.UserId)
    saveData:SetAsync("money", player.leaderstats.Gold.Value + player.Refund.Value)
    
    if player.Base.Value then
        local base = player.Base.Value
        local location = base:GetPrimaryPartCFrame()
        
        if base.Name == "Base" then
            -- your code
        elseif base.Name == "Base2" then
            -- your code
        elseif base.Name == "Base3" then
            -- your code
        elseif base.Name == "Base4" then
            -- your code
        end
        
        base:Destroy() -- now you can destroy the base
    end
end

game.Players.PlayerRemoving:Connect(SavePlayerData)
2 Likes

It works! Thank you so much for taking the time to help me fix this!

1 Like