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

How to fix "attempt to index upvalue 'timeName' (a string value)" error?

Hello! So I am trying to make DataSave for "playing time". But, I am getting error for 22nd line:

WHOLE SCRIPT:

local DataStore = game:GetService("DataStoreService"):GetDataStore("TimeDataStore")
local timeName = "Time"

game.Players.PlayerAdded:Connect(function(player)
	local load = player:WaitForChild("leaderstats")
	local leaderstats = player.leaderstats
	if load then
		leaderstats:WaitForChild("Cash")
		local Timing = Instance.new("IntValue")
		Timing.Parent = leaderstats
		Timing.Name = timeName
	end
	
	local ID = timeName.."-"..player.UserId
	local DataSave = nil
	
	pcall(function()
		DataSave = DataStore:GetAsync(ID)
	end)
	
	if DataSave ~= nil then
		timeName.Value = DataSave
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = timeName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[timeName].Value)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This server is shutting down")
		end
	end
	wait(3)
end)

and problem is:

if DataSave ~= nil then
		timeName.Value = DataSave
	end

I don't know, why is this happening, it's just a variable. Are there any solutions for that?

2 answers

on line 2 you declared timeName and set it's value to a string. now on line 22 you attempting to look up a property on timeName which once again holds a string value; so i'd recommend making the variable timing global or modify the "if statement" so you can set the value of the object in timing to the value of datasave

make sure you have another script, instance a leaderstats into the player when they join, or just put the code that instances a leaderstat upon joining over below player join expression

you can modify the code to make it show real time data of how much time player has spent in the game. but lemme know in case you need some help with that

local DataStore = game:GetService("DataStoreService"):GetDataStore("TimeDataStore")
local timeName = "Time"


game.Players.PlayerAdded:Connect(function(player)
	local access_key = timeName.."-"..player.UserId
	local savedData;

	local timeJoined = instance.new("IntValue",player)
	timeJoined.Name = "timeJoined"
	timeJoined.Value = tick()

	local stat = instance.new("Folder",player) 
	stat.Name = "leaderstats"

	local timeSpentOnGame = instance.new("IntValue",stat)
	timeSpentOnGame.Name = timeName


	local yay, nay = pcall(function()
		savedData = DataStore:GetAsync(access_key)
	end)


	if yay and savedData then
		print("Data successfully retrieved")
		timeSpentOnGame.Value = savedData

	elseif nay then
		print("there was an error:"..tostring(nay))
	else
		print("new player")
		timeSpentOnGame.Value = 0
	end


end)




function saveData(player)
	local mins, sec

	local access_key = local access_key = timeName.."-"..player.UserId
	local timeJoined =  player.timeJoined.Value
	local current_time = tick()
	local timeSpentOnGame = player.leaderstats.Time.Value

	local timeSpentInServer= currentTime - timeJoined
	
	timeSpentOnGame = timeSpentOnGame+timeSpentInServer

	dataStore:SetAsync(access_key,timeSpentOnGame)

end)







game.Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)




game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
	    saveData(player)
            player:Kick("This server is shutting down")
        end
    end
    wait(1)
end)

hopefully this accommodates for what you want. tell me if you need more help

1 comment

0

I understand that, but on other kinda same script (just different value), it's working okay: (I posted it below)

@Arsubia12

local DataStore = game:GetService("DataStoreService"):GetDataStore("CashDataStore")
local currencyName = "Cash"

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder", player)
	stats.Name = "leaderstats"
	wait(1)
	
	local Rank = Instance.new("StringValue", stats)
	Rank.Name = "Rank"
	Rank.Value = player:GetRoleInGroup(4998164)
	
	local Cash = Instance.new("IntValue", stats)
	Cash.Name = currencyName
	
	
	local ID = currencyName.."-"..player.UserId
	local savedData = nil
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		Cash.Value = savedData
	else
		Cash.Value = 50
	end
	
end)

3 comments

0

do you notice how you have an else statement if savedData is nil. but the other code doesn't have an exception for when DataSave is nil. so do some defensive programming to ensure all values are accommodated for

0

i just edited my answer; check it out

0

lemme know if you need more help

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