Indexing nil For A Value In A Table For Every Player

I Made A Table With Values For Every Player

The Issue Is If The Player Dies It Index nil For Every Value

image

I Couldn’t Understand the problem Soo If Anyone Knows How To Fix It Or Has Another Way To Do It I Would Appreciate it

this is the Module Script

local t = {}

for i, c in pairs(workspace:GetChildren()) do
	local h = c:FindFirstChild("Humanoid")

	if h then
		if h.Parent.Parent ~= workspace.Npc then
		t[c] = {
			Stun = false,
			Attack = false,
			Block = false,
			PerfectBlock = false,
			Dash = false,
			cd = false,
			cd1 = false,
			cd2 = false,
			Katana = false,
			}
		end
	end
end

workspace.ChildAdded:Connect(function(c)
	local h = c:WaitForChild("Humanoid")
	if h then
		t[c] = {
			Stun = false,
			Attack = false,
			Block = false,
			PerfectBlock = false,
			Dash = false,
			cd = false,
			cd1 = false,
			cd2 = false,
			Katana = false,
		}
	end
end)


workspace.ChildRemoved:Connect(function(c)
	local h = c:FindFirstChild("Humanoid")
	if h then
		t[c.Name] = nil
	end

end)


return t

And The Script That Uses The Value Is In StarterCharacterScripts + It’s A Client Script.

Extra Video

local t = {}

for i, c in pairs(workspace:GetChildren()) do
	local h = c:FindFirstChild("Humanoid")

	if h then
		if c.Parent ~= workspace.Npc then
		t[c.Name] = {
			Stun = false,
			Attack = false,
			Block = false,
			PerfectBlock = false,
			Dash = false,
			cd = false,
			cd1 = false,
			cd2 = false,
			Katana = false,
			}
		end
	end
end

workspace.ChildAdded:Connect(function(c)
	local h = c:WaitForChild("Humanoid")
	if h then
		t[c.Name] = {
			Stun = false,
			Attack = false,
			Block = false,
			PerfectBlock = false,
			Dash = false,
			cd = false,
			cd1 = false,
			cd2 = false,
			Katana = false,
		}
	end
end)


workspace.ChildRemoved:Connect(function(c)
	local h = c:FindFirstChild("Humanoid")
	if h then
		t[c.Name] = nil
	end

end)


return t

Edit:
The key you were setting to nil was c.Name (string) which is different from what the keys actually are which is model instances. The keys must be the same when setting them and deleting them…


Still Same Problem

I would appreciate it if you send the code where the t values are referenced.

local PV = PlayerValue[c.Name]

Edit:
The Module Script Name Is PlayerValue And I Already Made A Variable For it

local PlayerValue = require(game.ReplicatedStorage.PlayerValues)

Try ctrl+H to replace all the PV values with PlayerValue[c.Name] and see if that works.

1 Like

image
like this?

IT WORKS THANK YOU SOO MUCH

but if you don’t mind me asking, Do I have To Type That every time…?
can’t I just make a variable and go on with it?

1 Like

Since the key changes when the character is added and removed, it must be referenced every time the function is called. Rather than using PlayerValue[c.Name] every time, you can set it to a variable inside the UserInputService event function and use that… e.g:

function(key, istyping)
local var = PlayerValue[c.Name]
…var.cd…

1 Like

Please set it as a solution if it helped solve your problem.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.