You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to give the player items when they play for an amount of time
What is the issue? Include screenshots / videos if possible!
It keeps giving me the error " 19:32:15.327 ServerScriptService.Time Spent:24: attempt to index nil with ‘Backpack’ - Server - Time Spent:24’" when I reset.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have, but nothing helps me.
Code:
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("TotalTime")
local Players = game:GetService("Players")
local sessionData = {}
local AlwaysTrue = true
-- Function to save player's data
local function savePlayerData(playerUserId, plr)
playerData:SetAsync(playerUserId, plr.leaderstats.Time.Value)
print("Saved!")
end
-- Function to save player data on exit
local function Tool(dotool,plr,num,times,player)
local tool = dotool
if not game:GetService("Players"):GetPlayerFromCharacter(plr).Backpack:FindFirstChild(tool) and not plr:FindFirstChild(tool) then
if times == num then
local hint = Instance.new("Hint")
hint.Text = "Radio ".. tool:lower() .." has been rewarded! check it out in your backpack!"
hint.Parent = game:GetService("Players"):GetPlayerFromCharacter(plr).PlayerGui
game:GetService("Debris"):AddItem(hint, 5)
end
game:GetService("ServerStorage").Storage:FindFirstChild(tool):Clone().Parent = game:GetService("Players"):GetPlayerFromCharacter(plr).Backpack
end
end
local function GiveItem(plr, times)
if times ~= nil then
if times >= 3 then
Tool("Black",plr,3,times)
end
if times >= 8 then
Tool("Blue",plr,8,times)
end
end
end
local function saveOnExit(player)
local playerUserId = "Player_" .. player.UserId
savePlayerData(playerUserId, player)
end
local function onPlayerAdded(plr)
local playerUserId = "Player_" .. plr.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = 'leaderstats'
local timee = Instance.new("IntValue")
timee.Parent = leaderstats
timee.Name = 'Time'
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("TotalTime")
local playerUserId = "Player_" .. plr.UserId
local sessionData = {}
local data = playerData:GetAsync(playerUserId)
timee.Value = data
repeat
for Ihatecoding = 1,30 do
GiveItem(plr.Character, timee.Value)
wait(2)
end
local newTime = timee.Value + 1
timee.Value = newTime
until AlwaysTrue == false
end
Players.PlayerRemoving:Connect(saveOnExit)
Players.PlayerAdded:Connect(onPlayerAdded)
-- code before...
if not game:GetService("Players"):GetPlayerFromCharacter(plr).Backpack:FindFirstChild(tool) and not plr:FindFirstChild(tool) then
-- code after
Based on the error, game:GetService("Players"):GetPlayerFromCharacter(plr) is returning nil. This is because plr is nil, likely because the player’s character doesn’t exist anymore when resetting.
Since you need the player object to get the player’s Backpack (and not the character model), it would be better to directly pass the player object as an argument for the functions GiveItem and Tool. This would avoid issues with resetting since the character model won’t need to be retrieved anymore.
Changing GiveItem(plr.Character, timee.value) to GiveItem(plr, timee.Value) in the repeat-until loop might solve the problem.
Now I got error “ServerScriptService.Time Spent:24: attempt to index nil with ‘FindFirstChild’”
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("TotalTime")
local Players = game:GetService("Players")
local sessionData = {}
local AlwaysTrue = true
-- Function to save player's data
local function savePlayerData(playerUserId, plr)
playerData:SetAsync(playerUserId, plr.leaderstats.Time.Value)
print("Saved!")
end
-- Function to save player data on exit
local function Tool(dotool,plr,num,times,player)
local tool = dotool
if not plr.Backpack:FindFirstChild(tool) and not plr.Character:FindFirstChild(tool) then -- error
if times == num then
local hint = Instance.new("Hint")
hint.Text = "Radio ".. tool:lower() .." has been rewarded! check it out in your backpack!"
hint.Parent = plr.PlayerGui
game:GetService("Debris"):AddItem(hint, 5)
end
game:GetService("ServerStorage").Storage:FindFirstChild(tool):Clone().Parent = plr.Backpack
end
end
local function GiveItem(plr, times)
if times ~= nil then
if times >= 3 then
Tool("Black",plr,3,times)
end
if times >= 8 then
Tool("Blue",plr,8,times)
end
end
end
local function saveOnExit(player)
local playerUserId = "Player_" .. player.UserId
savePlayerData(playerUserId, player)
end
local function onPlayerAdded(plr)
local playerUserId = "Player_" .. plr.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = 'leaderstats'
local timee = Instance.new("IntValue")
timee.Parent = leaderstats
timee.Name = 'Time'
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("TotalTime")
local playerUserId = "Player_" .. plr.UserId
local sessionData = {}
local data = playerData:GetAsync(playerUserId)
timee.Value = data
repeat
for Ihatecoding = 1,30 do
GiveItem(plr, timee.Value)
wait(2)
end
local newTime = timee.Value + 1
timee.Value = newTime
until AlwaysTrue == false
end
Players.PlayerRemoving:Connect(saveOnExit)
Players.PlayerAdded:Connect(onPlayerAdded)
That is what that part of the code is trying to detect and stop the script if it is nil.
Detecting that is that if statement’s whole purpose
Edit: oops you meant the value “tool” doesn’t exist
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("TotalTime")
local Players = game:GetService("Players")
local sessionData = {}
local AlwaysTrue = true
-- Function to save player's data
local function savePlayerData(playerUserId, plr)
playerData:SetAsync(playerUserId, plr.leaderstats.Time.Value)
print("Saved!")
end
-- Function to save player data on exit
local function Tool(tool,plr,num,times,player)
if plr.Character ~= nil then
if plr.Backpack:FindFirstChild(tool) ~= nil and plr.Character:FindFirstChild(tool) ~= nil then
if times == num then
local hint = Instance.new("Hint")
hint.Text = "Radio ".. tool:lower() .." has been rewarded! check it out in your backpack!"
hint.Parent = plr.PlayerGui
game:GetService("Debris"):AddItem(hint, 5)
end
game:GetService("ServerStorage").Storage:FindFirstChild(tool):Clone().Parent = plr.Backpack
end
end
end
local function GiveItem(plr, times)
if times ~= nil then
if times >= 3 then
Tool("Black",plr,3,times)
end
if times >= 8 then
Tool("Blue",plr,8,times)
end
end
end
local function saveOnExit(player)
local playerUserId = "Player_" .. player.UserId
savePlayerData(playerUserId, player)
end
local function onPlayerAdded(plr)
local playerUserId = "Player_" .. plr.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = 'leaderstats'
local timee = Instance.new("IntValue")
timee.Parent = leaderstats
timee.Name = 'Time'
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("TotalTime")
local playerUserId = "Player_" .. plr.UserId
local sessionData = {}
local data = playerData:GetAsync(playerUserId)
timee.Value = data
repeat
for Ihatecoding = 1,30 do
GiveItem(plr, timee.Value)
wait(2)
end
local newTime = timee.Value + 1
timee.Value = newTime
until AlwaysTrue == false
end
Players.PlayerRemoving:Connect(saveOnExit)
Players.PlayerAdded:Connect(onPlayerAdded)
The error means that either plr.Backpack or plr.Character is nil since they’re indexed by FindFirstChild(), not necessarily the tool.
The issue might be in the second part of the if-statement, plr.Character:FindFirstChild(tool), since this is still referencing the player’s character model (which might not exist).
Try checking if the character model exists before looking for the tool inside it. Your new code seems to be on the right track, but you could remove if plr.Character ~= nil then and rewrite the second part of the if-statement as…
... and not (plr.Character and plr.Character:FindFirstChild(tool)) ...
Otherwise, let us know if the code you tried works.
I have it so it prints when the function is called now, I am not getting the tool, but the error is gone.
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("TotalTime")
local Players = game:GetService("Players")
local sessionData = {}
local AlwaysTrue = true
-- Function to save player's data
local function savePlayerData(playerUserId, plr)
playerData:SetAsync(playerUserId, plr.leaderstats.Time.Value)
print("Saved!")
end
-- Function to save player data on exit
local function Tool(tool,plr,num,times,player)
print("Giving tool")
if plr.Backpack:FindFirstChild(tool) ~= nil and not (plr.Character and plr.Character:FindFirstChild(tool)) then
if times == num then
local hint = Instance.new("Hint")
hint.Text = "Radio ".. tool:lower() .." has been rewarded! check it out in your backpack!"
hint.Parent = plr.PlayerGui
game:GetService("Debris"):AddItem(hint, 5)
end
game:GetService("ServerStorage").Storage:FindFirstChild(tool):Clone().Parent = plr.Backpack
end
end
local function GiveItem(plr, times)
-- if times ~= nil then
if times >= 3 then
Tool("Black",plr,3,times)
end
if times >= 8 then
Tool("Blue",plr,8,times)
end
end
--end
local function saveOnExit(player)
local playerUserId = "Player_" .. player.UserId
savePlayerData(playerUserId, player)
end
local function onPlayerAdded(plr)
local playerUserId = "Player_" .. plr.UserId
local leaderstats = Instance.new("Folder")
leaderstats.Parent = plr
leaderstats.Name = 'leaderstats'
local timee = Instance.new("IntValue")
timee.Parent = leaderstats
timee.Name = 'Time'
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("TotalTime")
local playerUserId = "Player_" .. plr.UserId
local sessionData = {}
local data = playerData:GetAsync(playerUserId)
timee.Value = data
repeat
for Ihatecoding = 1,30 do
GiveItem(plr, timee.Value)
wait(2)
end
local newTime = timee.Value + 1
timee.Value = newTime
until AlwaysTrue == false
end
Players.PlayerRemoving:Connect(saveOnExit)
Players.PlayerAdded:Connect(onPlayerAdded)
I’d recommend having your code reviewed at #help-and-feedback:code-review. Although your code works, there are a few things you could improve which might help you avoid problems in the future.