Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

attempt to index local cashdata (a number value)?

And no, before you link me to some wack thread, I have read up on each one and none of them have the right solution, for they are completely different matters.

game.Players.PlayerAdded:Connect(function(plr)
	local copieddata = script.Data:Clone()
	copieddata.Parent = plr
	local data = game:GetService("DataStoreService")
    local cashdata = data:GetDataStore("Cash")
		local success, cashdata = pcall(function()
			return cashdata:GetAsync(plr)
 		end)
 
 		if success then
			if cashdata == nil then
			cashdata:SetAsync(plr, 0)
			end
			copieddata.Cash.Value = cashdata
 	end


	--data
	
	
	local copiedplotinfo = script.PlotInformation:Clone()
	copiedplotinfo.Parent = plr
	if workspace.Plots.Plot1.Owner.Value == "Nil" then
		copiedplotinfo.Plot.Value = "Plot1"
		workspace.Plots.Plot1.Owner.Value = plr.Name
	elseif workspace.Plots.Plot2.Owner.Value == "Nil" then
		copiedplotinfo.Plot.Value = "Plot2"
		workspace.Plots.Plot2.Owner.Value = plr.Name
	elseif workspace.Plots.Plot3.Owner.Value == "Nil" then
		copiedplotinfo.Plot.Value = "Plot3"
		workspace.Plots.Plot3.Owner.Value = plr.Name
	elseif workspace.Plots.Plot4.Owner.Value == "Nil" then
		copiedplotinfo.Plot.Value = "Plot4"
		workspace.Plots.Plot4.Owner.Value = plr.Name
	end
	--plot placement
	
	
	local currentplot = copiedplotinfo.Plot.Value
	local plot = workspace.Plots:FindFirstChild(currentplot)
	local function farmincome() --farm gainer stuff
		local farm = plot.Farm
		local farmlvl = farm.LVL.Value
		local income = 0
		if farmlvl == 1 then
			income = 50
		end
		local Cash = copieddata.Cash
		Cash.Value = Cash.Value + income
		print("Gave farm cash respectively. New cash=".. Cash.Value)
		print("Amount of cash recieved=".. income)
		cashdata:SetAsync(plr, Cash.Value)
	end
	
	local function westernshootoutscoutedition()
		--MAKE SCOUT DELAY TIME AND CASH GAIN ETC
	end
	
	while true do
		wait(5)
		farmincome() --farm income delay and thing
		wait()
	end
	
	--next oen here
	--farms, zombie dmg, etc. (active slots r used for like showing if it actually function)
end)

ERROR AT LINE 52!

1 answer

✓ Accepted answer

At line 06, you attempted to define cashdata again as the data in the data store, which in this case is a number. So when you attempted to set the data at line 52, you're actually calling SetAsync on a number. You need to use a different variable name for the variable to not override itself.

game.Players.PlayerAdded:Connect(function(plr)
    local copieddata = script.Data:Clone()
    copieddata.Parent = plr
    local data = game:GetService("DataStoreService")
    local cashdata = data:GetDataStore("Cash")
        local success, cash = pcall(function()
            return cashdata:GetAsync(plr)
        end)

        if success then
            if cash == nil then
            cashdata:SetAsync(plr, 0)
            end
            copieddata.Cash.Value = cash
    end


    --data


    local copiedplotinfo = script.PlotInformation:Clone()
    copiedplotinfo.Parent = plr
    if workspace.Plots.Plot1.Owner.Value == "Nil" then
        copiedplotinfo.Plot.Value = "Plot1"
        workspace.Plots.Plot1.Owner.Value = plr.Name
    elseif workspace.Plots.Plot2.Owner.Value == "Nil" then
        copiedplotinfo.Plot.Value = "Plot2"
        workspace.Plots.Plot2.Owner.Value = plr.Name
    elseif workspace.Plots.Plot3.Owner.Value == "Nil" then
        copiedplotinfo.Plot.Value = "Plot3"
        workspace.Plots.Plot3.Owner.Value = plr.Name
    elseif workspace.Plots.Plot4.Owner.Value == "Nil" then
        copiedplotinfo.Plot.Value = "Plot4"
        workspace.Plots.Plot4.Owner.Value = plr.Name
    end
    --plot placement


    local currentplot = copiedplotinfo.Plot.Value
    local plot = workspace.Plots:FindFirstChild(currentplot)
    local function farmincome() --farm gainer stuff
        local farm = plot.Farm
        local farmlvl = farm.LVL.Value
        local income = 0
        if farmlvl == 1 then
            income = 50
        end
        local Cash = copieddata.Cash
        Cash.Value = Cash.Value + income
        print("Gave farm cash respectively. New cash=".. Cash.Value)
        print("Amount of cash recieved=".. income)
        cashdata:SetAsync(plr, Cash.Value)
    end

    local function westernshootoutscoutedition()
        --MAKE SCOUT DELAY TIME AND CASH GAIN ETC
    end

    while true do
        wait(5)
        farmincome() --farm income delay and thing
        wait()
    end

    --next oen here
    --farms, zombie dmg, etc. (active slots r used for like showing if it actually function)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.