Help with this script!

I dont know whay my script isnt saving the tools… please help if you spot an error

local toolstorage = game.ServerStorage
local blacklist = {"ClassicSword", "RocketLauncher"}

local data = game:GetService("DataStoreService"):GetDataStore("Player_Tools")
local players = game.Players

function savedata(plr)
	local a = {}
	for _, v in pairs(plr.StarterGear:GetChildren()) do
		if table.find(blacklist, v.Name) then
			
		else
			table.insert(a, v.Name)
		end
	end
	if a[1] then
		data:SetAsync(plr.userId, a)
	end
end

players.PlayerAdded:Connect(function(plr)
	local b = data:GetAsync(plr.userId)
	if b then
		for _, v in pairs(b) do
			toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
			toolstorage:FindFirstChild(v):Clone().Parent = plr.StarterGear
		end
	end
	while true do
		wait(300)
		savedata(plr)
	end
end)

players.PlayerRemoving:Connect(function(plr)
	if plr then
		savedata(plr)
	end
end)

game:BindToClose(function()
	for _, plr in pairs(players:GetChildren()) do
		savedata(plr)
	end
end)```

Tools are in Backpack not StarterGear so in your SaveData function the for loop need to look in player backpack

hmm do you see anything else wrong with the script? i tried to do what u said but it still appears to be broken.

Try this:

local toolstorage = game.ServerStorage
local blacklist = {"ClassicSword", "RocketLauncher"}

local data = game:GetService("DataStoreService"):GetDataStore("Player_Tools")
local players = game.Players

function savedata(plr)
	local a = {}
	for _, v in pairs(plr.Backpack:GetChildren()) do
		if table.find(blacklist, v.Name) then
			
		else
			table.insert(a, v.Name)
		end
	end
	if a[1] then
		data:SetAsync(plr.userId, a)
	end
end

players.PlayerAdded:Connect(function(plr)
	local b = data:GetAsync(plr.userId)
	if b then
		for _, v in pairs(b) do
			toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
		end
	end
	while true do
		wait(300)
		savedata(plr)
	end
end)

players.PlayerRemoving:Connect(function(plr)
	if plr then
		savedata(plr)
	end
end)

game:BindToClose(function()
	for _, plr in pairs(players:GetChildren()) do
		savedata(plr)
	end
end)

Also please try sending the errors you’re getting so we can help you better!
Hope this will help :slight_smile:

sorry, but that also doesn’t work for some reason. also i would send an error message, but there isnt one for some reason

Two Questions,

#1 Do you know how to code
#2 Did you make this code yourself

I would defiantly suggest not to take other people’s code that you don’t understand.

You could also use print() for debugging purposes to find out the problem.

1 Like

Are you testing this in Studio? If so, try publishing your game and turn on Enable Studio Access to API Services.

Answer 1, i moderatly know how to code.
Answer 2, me and my friend are making a game and he brought up this code to me, but since he doesnt have a roblox forums account to publish this with.

ill try that right now, i think that might help

yeah i tried doing that, and i got no luck

Hmm, ill have to refigure some stuff, so ill try another way todo this. All I wanted to do was make it so that i had a tool save system but had a blacklist. Thanks to whoever was kind enough to help!

I’m not sure if this is the problem, but I would try removing this autosave loop. I have no autosave feature (only PlayerRemoving) in my game and it saves data fine.

while true do
  wait(300)
  savedata(plr)
end

Ill try this right now, my roblox studio is acting up rn

wait this really worked for you? if it did please explain what u did to make it work.

I’m currently testing your script. (I’m new to tables)


I did say this…

I think that this is a bit more optimized, line-wise.

while wait(300) do savedata(plr) end

Changed a couple things and then found an output error coming from these lines… what is in your ServerStorage? The script expects the saved tools to also be in the ServerStorage.

toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
toolstorage:FindFirstChild(v):Clone().Parent = plr.StarterGear

This is the slightly modified Script:

local toolstorage = game.ServerStorage
local blacklist = {"ClassicSword", "RocketLauncher"}

local datastoreservice = game:GetService("DataStoreService")
local data = datastoreservice:GetDataStore("Player_Tools")
local players = game.Players

local function savedata(plr)
	local a = {}
	for _, v in pairs(plr.Backpack:GetChildren()) do
		if table.find(blacklist, v.Name) then

		else
			table.insert(a, v.Name)
		end
	end
	
	data:SetAsync(plr.userId, a)
	
	print ("saved")

end

players.PlayerAdded:Connect(function(plr)
	local b = data:GetAsync(plr.userId)
	if b then
		for _, v in pairs(b) do
			toolstorage:FindFirstChild(v):Clone().Parent = plr.Backpack
			toolstorage:FindFirstChild(v):Clone().Parent = plr.StarterGear
		end
	end
	print ("loaded")
end)

players.PlayerRemoving:Connect(function(plr)
	if plr then
		savedata(plr)
	end
end)

Putting the tool in the ServerStorage fixes it and makes the script work properly (saving and loading your tools!)

image

Maybe its just the game but my tools wont save.

Also this is what’s in my ServerStorage:

snippet

Your own blacklisting system does work. Just check that the tool names are the same…

You can also add a print("blacklisted tool") underneath the table.find line inside the if statement.

Make sure that you are not trying to save blacklisted items… saving/loading works fine for me.