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 comment
Well that seems to have fixed the problem. Thanks.