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 field '?' a nil value?

Hi guys. I am making a data system where if I touch a brick it increments the data but it prints nil when clearly it shouldn't.

This is the module script:

local Data = {}
local PlayerData = {} -- player data table


function Data.AddPlayer(player, data) -- adds the player in the table
	PlayerData[player] = data
end


function Data.ReturnData(player) -- returns or gives the player to the script
	return PlayerData[player]
end


function Data.RemovePlayer(player) -- removes player in the table
	PlayerData[player] = nil
end


function Data.IncrementNovas(player, value)
	PlayerData[player]["Novas"] = PlayerData[player]["Novas"] + value
end


function Data.IncrementLevel(player, value)
	PlayerData[player]["Level"] = PlayerData[player]["Level"] + value
end


return Data

This is the script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local DataModule = require(ServerScriptService.DataModuleScript)
local ExpGiver = ReplicatedStorage.Resources.Bindables.BindableEvents:WaitForChild("ExpGiverBindableEvent")

local data = {
	Level = 1;
	Novas = 100
}

Players.PlayerAdded:Connect(function(player)
	DataModule.AddPlayer(player, data)
	
	ExpGiver.Event:Connect(function(player, dataname, value)
		if dataname == "Novas" then
			DataModule.IncrementNovas(player, value)
		elseif dataname == "Level" then
			DataModule.IncrementLevel(player, value)
		end
	end)
	
end)

Players.PlayerRemoving:Connect(function(player)
	DataModule.RemovePlayer(player)
end)

And this is the touched script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Event = ReplicatedStorage.Resources.Bindables.BindableEvents:WaitForChild("ExpGiverBindableEvent")


script.Parent.Touched:Connect(function(hit)
	-- sees if what touched it is a player
	local player = hit.parent 
	local touched = Players:GetPlayerFromCharacter(player)
	if not touched then return end
	Event:Fire(player, "Level", 1) -- return the player, what value, and the value
end)

but when I touch the brick, the output is:

attempt to index field '?' (a nil value) at line 26 of module script

1 answer

✓ Accepted answer

The issue is that you pass the character model (instead of the actual player) in the Touched script. I'm assuming that the data is inside the PlayerInstance, and not the character. The reason I assume this is because you initialize the player in the Dictionary using the player as the index, not their character. If it doesn't work, hit me up on Discord (GenScript#7676) and we'll get it working.

Fix:

-- Touched script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Event = ReplicatedStorage.Resources.Bindables.BindableEvents:WaitForChild("ExpGiverBindableEvent")


script.Parent.Touched:Connect(function(hit)
    -- sees if what touched it is a player
    local player = hit.parent 
    local touched = Players:GetPlayerFromCharacter(player)
    if not touched then return end
    Event:Fire(touched, "Level", 1) -- return the player, what value, and the value
end)

1 comment

0

Well that seems to have fixed the problem. Thanks.

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