Attempt to index a nil value issue related to datastore2

Right now i am in the midst of working on a script which upon the enemy getting killed it will update the datastore based off a values table.

Every other script works except for this one:

local lasthitplr = ""
local tweenservice = game:GetService('TweenService')
local clone = script.Parent:Clone('   ')
local origposition = script.Parent:GetPrimaryPartCFrame()
local effect = false
local datastore2 = require(game.ServerScriptService.DataStore2)
local MainKey = "DataStoreKey"

for i, v in pairs (script.Parent:GetChildren()) do
	if v:IsA('Part') then
	v.Touched:Connect(function(hit)
		if game.Players:FindFirstChild(hit.Parent.Parent.Name) then
				if effect == false then
				lasthitplr = hit.Parent.Parent.Name
			end
		end
	end)
	end
end

local function effect()
	local userdata = datastore2(MainKey, game.Players[lasthitplr]):Get()
	local Values = datastore2("Values", game.Players[lasthitplr])
	effect = true
	local char = game.Workspace:WaitForChild(lasthitplr)
	local moneymin = tonumber((script.Parent.Humanoid.MaxHealth * 0.15) * game.ReplicatedStorage:WaitForChild(lasthitplr.."'s Values").CoinMultiplier.Value) --the minimum amount of money the enemy will drop
	local expmin = tonumber((script.Parent.Humanoid.MaxHealth * 0.15))
	local expgain = math.floor(math.random(expmin, expmin * 1.35))
	local coingain = math.floor(math.random(moneymin, moneymin * 1.35))
	for i = 1, 4 do
		local partfx = Instance.new('Part')
		partfx.Anchored = false
		partfx.CanCollide = true
		partfx.Transparency = 1
		partfx.Size = Vector3.new(.1,0.1,.1)
		partfx.Name = "coineffect"
		partfx.Parent = script	
		partfx.Position = script.Parent.Head.Position + Vector3.new(0,1,0)
		local fxclone = game.ReplicatedStorage.Effects.Coin:Clone()
		fxclone.Parent = partfx
		wait()
	end
	script.Parent = game.Workspace
	wait(1)
	for i, v in pairs(script:GetChildren()) do
		v.Anchored = true
		v.CanCollide = false
	end
	local parts = script:GetChildren()
	for i = 0, 1, .01 do
		parts[1].CFrame = parts[1].CFrame:Lerp(char.HumanoidRootPart.CFrame, i)
		parts[2].CFrame = parts[2].CFrame:Lerp(char.HumanoidRootPart.CFrame, i)
		parts[3].CFrame = parts[3].CFrame:Lerp(char.HumanoidRootPart.CFrame, i)
		parts[4].CFrame = parts[4].CFrame:Lerp(char.HumanoidRootPart.CFrame, i)
		wait()
		for i, v in pairs(parts) do
			v.Touched:Connect(function(hit)
				if hit.Name == "HumanoidRootPart" then
					v:Destroy()
				end
			end)
		end
	end
	for i, v in pairs(script:GetChildren()) do
		v:Destroy()
	end
	if game:GetService('MarketplaceService'):PlayerOwnsAsset(game.Players:FindFirstChild(lasthitplr), 7031512) then
	userdata.Values.Experience = userdata.Values.Experience + expgain * 2
	else
	userdata.Values.Experience = userdata.Values.Experience + expgain
	end
	Values:Set(userdata.Values)
	if game:GetService('MarketplaceService'):PlayerOwnsAsset(game.Players:FindFirstChild(lasthitplr), 7031507) then
		userdata.Values.Coins = userdata.Values.Coins + coingain * 2
		Values:Set(userdata.Values)
		game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value = game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value - coingain * 2
		for i = game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value, game.ReplicatedStorage:WaitForChild(lasthitplr.."'s Values").Coins.Value + (coingain * 2), coingain * .01 do
			wait()
			game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value = i
		end
		else
		userdata.Values.Coins = userdata.Values.Coins + coingain
		Values:Set(userdata.Values)
		game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value = game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value - coingain 
		for i = game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value, game.ReplicatedStorage:WaitForChild(lasthitplr.."'s Values").Coins.Value + coingain, coingain * .01 do
			wait()
			game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value = i
		end
	end
	script:Destroy()
end

script.Parent.Humanoid.Died:Connect(function()
	repeat wait() until lasthitplr ~= ""
	effect()
end)

i don’t know what to do, error is occuring on this line:

userdata.Values.Experience = userdata.Values.Experience + expgain

It seems you havent defined a default value for userdata,

lets say a player joins a game for the first time, so they don’t have any data. that value is nil

:Get() looks for the argument to set a default value there. if there is none, then it is nil.
this is likely your issue, put a default value here then try again.

did that, it cleared one of the problems but another one arised.

the line occurs at

game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value = i

the error is: Attempt to index a nil value, again

Sounds like a few things could be the issue

lasthitplr…'s Values doesn’t exist
Coins doesn’t exist

also if you don’t know what attempt to index a nil value is, i can explain it if u want

yeah i don’t really know what it means, please explain it if you wouldn’t mind.

also i’m fairly sure the problem would lie at lasthitplr doesn’t exist, just need to find a way to fix that.

ok so basically nil is nothing
attempt to index a nil value is basically when you try to reference something that is nil (nothing)

here’s an example
in this example there is only a baseplate in the workspace

print(workspace.Baseplate.Name) -- would print Baseplate since thats the name

print(RandomPartThatDoesntExist.Name) -- error, random part doesn't exist so it's nil

output of above code should be attempt to index RandomPartThatDoesntExist a nil value

edit: can you also try printing out the last player?

Ohhh okay, understood. So what i need to do is basically find a better way to detect which player hit the enemy last?

maybe you have it, try printing it out on the line before

alright, gonna give that a shot now.

everything works fine now? that’s weird.
Thanks for your help again dude, you rock.

1 Like