Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I am having hell with these scripts! Can any give any advice with saving backpack items?

I have tried and tried this, I cannot for the life of me figure it out! I have a script set up so a player can buy items, then they are taken from the replicated storage as a clone, and into the players starter gear and backpack. Here is that script

script.Parent.MouseButton1Click:connect(function()
	local RS = game:GetService('ReplicatedStorage')
	local item = RS:WaitForChild('GrappleHook')
	local price = 0
	local player = game.Players.LocalPlayer
	local stats = player:WaitForChild('leaderstats')
	
	if stats.Cash.Value >= price then
		stats.Cash.Value = stats.Cash.Value - price
		local cloned = item:Clone()
		local cloned2 = item:Clone()
		cloned2.Parent = player.Backpack
		cloned.Parent = player.StarterGear
	end
end)

and that works just fine,, one time. i dont want the player to have to purchase a new one everytime they die, that would be frustrating to everyone. But my problem is that when players die the tool just disappears, they have to re purchase the item. Here is the script im using to try to save the tool. Do you see any problems that could be getting in the way, any pointers you could give me? here is the data store script


local RunService = game:GetService("RunService")
local ToolData = game:GetService("DataStoreService"):GetDataStore("ToolData")

local ToolLocation = game.ServerStorage.Tools -- The location where all your tools are saved

function GetToolByName(toolName)
	for _, tool in pairs(ToolLocation:GetChildren()) do
		if tool.Name == toolName then return tool end
	end
end

function IsRegisteredTool(tool)
	for _, playerTool in pairs(ToolLocation:GetChildren()) do
		if playerTool.Name == tool.Name then return true end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	repeat wait() until player.Character
	local dataKey = "tool-data_"..player.UserId
	if ToolData:GetAsync(dataKey) then
		for i, toolName in pairs(ToolData:GetAsync(dataKey)) do
			local newStarterTool = GetToolByName(ToolData:GetAsync(dataKey)[i]):Clone()
			local newTempTool = GetToolByName(ToolData:GetAsync(dataKey)[i]):Clone()
			newStarterTool.Parent = player.StarterGear
			newTempTool.Parent = player.Backpack
		end
		ToolData:SetAsync(dataKey, {})
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local dataKey = "tool-data_"..player.UserId
	local playerTools = {}
	for _, tool in pairs(player.StarterGear:GetChildren()) do
		if tool:IsA("Tool") and IsRegisteredTool(tool) then table.insert(playerTools, 1, tool.Name) end
	end
	if RunService:IsStudio() or #playerTools < 1 then return end
	ToolData:SetAsync(dataKey, playerTools)
end)

thanks everyone!!

1 answer

What I would is that I would make it so that it will change a string value inside of that player that shows that what weapons the person has purchased. For example, I have a string value in the player.

script.Parent.MouseButton1Click:connect(function()
	--Do what is needed
	StringValue = game.Players.LocalPlayer.StringValue
	StringValue.Value = "ToolName"
end

Then you can make a script when the player dies it gets the item back again like this

plr.Died:connect(function()
	local clone = game.ReplicatedStorage:FindFirstChild(plr.StringValue.Value):Clone()
	clone.Parent = plr.Backpack
end)

3 comments

0

Okay ill try that real quick, ill let you know what happens, thanks!

0

Where would I place the 2nd script? Workspace?

0

The first script is where you put the purchase script(Make it FE compatible), The second one make a normal script and put it in StarterCharacterScripts

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