I keep getting an error and I don't understand why

So basically I am doing a database thing trying to create clicking simulator everything looked great until I tried saving rebirths too. The error is:

23:18:39.311 ServerScriptService.Main Script Holder:42: attempt to index number with ‘clicks’ - Server - Main Script Holder:42
23:18:39.311 Stack Begin - Studio
23:18:39.311 Script ‘ServerScriptService.Main Script Holder’, Line 42 - Studio - Main Script Holder:42
23:18:39.312 Stack End - Studio

And the code is:

players.PlayerAdded:Connect(function(plr)

local lb = Instance.new(“Folder”)
lb.Name = “leaderstats”
lb.Parent = plr

local clicks = Instance.new(“IntValue”)
clicks.Name = “Clicks”
clicks.Parent = lb

local rebirths = Instance.new(“IntValue”)
rebirths.Name = “Rebirths”
rebirths.Parent = lb

local cAmount

local success, err = pcall(function()
cAmount = data:GetAsync(“Clicks_”…plr.UserId)
end)

if success then
if cAmount then
clicks.Value = cAmount.clicks
rebirths.Value = cAmount.rebirths
else
clicks.Value = 0
rebirths.Value = 0
end
else
warn(err)
end

end)

players.PlayerRemoving:Connect(function(plr)
local cAmount = {
clicks = plr.leaderstats.Clicks.Value;
rebirths = plr.leaderstats.Rebirths.Value;
}

local success, err = pcall(function()
data:SetAsync(“Clicks_”…plr.UserId, cAmount)
end)

if success then
print(“DTB COOL”)
else
warn(err)
end
end)

can you please use code blocks?

but im reading your code

edit 1: what is cAmount?

I’m just formatting your code so we can read it easier:

players.PlayerAdded:Connect(function(plr)

local lb = Instance.new(“Folder”)
lb.Name = “leaderstats”
lb.Parent = plr

local clicks = Instance.new(“IntValue”)
clicks.Name = “Clicks”
clicks.Parent = lb

local rebirths = Instance.new(“IntValue”)
rebirths.Name = “Rebirths”
rebirths.Parent = lb

local cAmount

local success, err = pcall(function()
cAmount = data:GetAsync(“Clicks_”…plr.UserId)
end)

if success then
if cAmount then
clicks.Value = cAmount.clicks
rebirths.Value = cAmount.rebirths
else
clicks.Value = 0
rebirths.Value = 0
end
else
warn(err)
end

end)

players.PlayerRemoving:Connect(function(plr)
local cAmount = {
clicks = plr.leaderstats.Clicks.Value;
rebirths = plr.leaderstats.Rebirths.Value;
}

local success, err = pcall(function()
data:SetAsync(“Clicks_”…plr.UserId, cAmount)
end)

if success then
print(“DTB COOL”)
else
warn(err)
end
end)
1 Like

First I noticed that you said:

datastore:SetAsync("Clicks_"...plr.UserId, cAmount)

Concatonating uses 2 dots not three. So when you say “Clicks_”…plr.UserId, you are not concatonating it right so it might cause an error.

here is a easier and more readable version of what you made i think:

local DS = game:GetService("DataStoreService")

local SaveClicks = DS:GetDataStore("SaveClicks")
local SaveRebirths = DS:GetDataStore("SaveRebirths")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderboard  = Instance.new("Folder", Player)
	Leaderboard.Name = "leaderstats"
	
	local Clicks = Instance.new("IntValue", Leaderboard)
	Clicks.Name = "Clicks"
	Clicks.Value = 0 or SaveClicks:GetAsync(Player.UserId)
	
	local Rebirths = Instance.new("IntValue", Leaderboard)
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0 or SaveRebirths:GetAsync(Player.UserId)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Clicks = Player.leaderstats.Clicks.Value
	local Rebirths = Player.leaderstats.Rebirths.Value
	
	SaveClicks:SetAsync(Player.UserId, Clicks)
	SaveRebirths:SetAsync(Player.UserId, Rebirths)
end)
1 Like