i have been following a book and have got to a point where it hasn't worked. when I touch the object it returns PlayerStatManager:changeStat returned nill. any help would be appreciated also sorry if you have seen this same Q before.
Module Script
local PlayerStatManager = {}
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local AUTOSAVE_INTERVAL = 60
local DATASTORE_RETIES = 3
local sessionData = {}
function PlayerStatManager:ChangeStat(player,statName,changeValue)
local playerUserId = "Player_"..player.UserId
assert(typeof(sessionData[playerUserId][statName])== typeof(changeValue), "changestat error type doesnt match")
if typeof(sessionData[playerUserId][statName]== "number") then
sessionData[statName] = sessionData[playerUserId][statName]+changeValue
else
sessionData[playerUserId][statName] = changeValue
end
end
local function dataStoreRetry(dataStoreFunction)
local tries = 0
local success = true
local data = nil
repeat
tries = tries + 1
success = pcall(function() data = dataStoreFunction()end)
if not success then
wait(1)
end
until tries == DATASTORE_RETIES or success
if not success then
error("could not access DataStore")
end
return success, data
end
local function getPlayerData(player)
return dataStoreRetry(function()
return playerData:GetAsync(player.UserId)
end)
end
local function savePlayerData(player)
if sessionData[player] then
return dataStoreRetry(function()
return playerData:SetAsync(player.UserId,sessionData[player])
end)
end
end
local function setupPlayerData(player)
local success, data = getPlayerData(player)
if not success then
sessionData[player] = false
else
if not data then
sessionData[player] = {Aggressive = 0, AggressiveXp = 0, Timid = 0, TimidXp = 0, Large = 0, LargeXp = 0, Small = 0, SmallXp = 0, Hunting = 0}
savePlayerData(player)
else
sessionData[player] = data
end
end
end
local function autosave()
while wait(AUTOSAVE_INTERVAL) do
for player, data in pairs(sessionData) do
savePlayerData(player)
end
end
end
game.Players.PlayerAdded:Connect(setupPlayerData)
game.Players.PlayerRemoving:Connect(function(player)savePlayerData(player) sessionData[player] = nil
end)
spawn(autosave)
return PlayerStatManager
External scipt
local PlayerStatManager require(game.ServerScriptService.playerstatsmanager)
local huntingstart = script.Parent
local function IncreaseHunting(part)
local hum = part.Parent:FindFirstChild("Humanoid")
local plname = part.Parent.Name
if hum then
local player = game.Players:FindFirstChild(plname)
PlayerStatManager:ChangeStat(player,"Hunting",5)
huntingstart:Destroy()
end
end
huntingstart.Touched:Connect(IncreaseHunting)

6 comments
Why you just didn't edited the question?
i couldnt find out how new to the website and i have just seen the edit button as i was typing this
Also: The answer is because you don't return nothing on the function
which function? and how would i return something?
line 11. To return a value simply use 'return '
A bit more context and maybe an example on how to use it in my code. Just stating what is wrong and giving the bare minimum answer isnt helping.