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

How do I make sure the purchased tool stays within the player's inventory after death?

In my previous question, I asked on how I would make sure a tool will always remain in the player's inventory even after death.

The solution someone gave me was to use :WaitForChild('StarterGear').

This is my shop script, which is a LocalScript in an ImageButton.

script.Parent.MouseEnter:Connect(function()
	game.StarterPlayer.Mouse_hover:Play()
end)

local canPurchase = false

script.Parent.MouseButton1Click:Connect(function()
	game.StarterPlayer.Select:Play()
	local confirmPurchase = script.Parent.Parent.Parent.ConfirmPurchase:Clone()
	confirmPurchase.Visible = true
	confirmPurchase.Parent = script.Parent.Parent.Parent
	confirmPurchase.Yes.MouseButton1Click:Connect(function()
		canPurchase = true
		if game.Players.LocalPlayer:WaitForChild('leaderstats').Vertigo.Value >= 25000 then
			game.StarterPlayer.SFX_SelectAvailable:Play()
			game.Players.LocalPlayer:WaitForChild('leaderstats').Vertigo.Value = game.Players.LocalPlayer:WaitForChild('leaderstats').Vertigo.Value - 10000
			local itemClone = game.ReplicatedStorage.Guns.Sniper:Clone()
			itemClone.Parent = game.Players.LocalPlayer.Backpack
			local itemClone2 = game.ReplicatedStorage.Guns.Sniper:Clone()
			itemClone2.Parent = game.Players.LocalPlayer:WaitForChild('StarterGear')
			game.ReplicatedStorage.Purchased:FireServer(25000)
			confirmPurchase:remove()
			canPurchase = false
		else
			game.ReplicatedStorage.NotEnoughVertigo:FireServer()
		end
	end)
end)

Terribly sorry if it's hard to read, it's the formatting.

Can anyone help me fix this issue? I've had it for a very long time.

Regards, Sensei.

2 answers

I would personally use DataStores for this. DataStores are tables that persist between sessions. To insert/modify data in a DataStore array you must use SetAsync, UpdateAsync and RemoveAsync.

SetAsync creates a new value and modifies it to the parameters specified.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:SetAsync("ExamplePlayer", 50)

This creates a new value ExamplePlayer and sets it's corresponding value to 50.

UpdateAsync modifies an existing value to the parameters specified.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:UpdateAsync("ExamplePlayer", 50)

This changes ExamplePlayer's corresponding value to 50

RemoveAsync removes a value in the array.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:RemoveAsync("ExamplePlayer")

This removes ExamplePlayer and its corresponding value

For more see Data Stores.

2 comments

0

Instead of saving numbers, bools and stings, is it possible to save objects by simply putting in ExampleStore:SetAsync("Player",object)?

0

No, since an ObjectValue just stores a link of sorts to an object and objects are not persistent between sessions. Even though you aren’t exactly storing between sessions DataStores are designed to.

Just pop this in ServerScriptService

--[[

MAde by Bssiemeow!
--]]

local rs = game:GetService("ReplicatedStorage")
local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("saveStore")
local library = rs:WaitForChild("Library")

--< directory functions

local dir = {}

local function edit(player, list)
	dir[player.Name] = list
end

local function setup(player, list)
	for i = 1, #list do
		local tool = library:FindFirstChild(list[i])
		if tool then
			local clone = tool:Clone()
			clone.Parent = player.Backpack
		else
			print(list[i] .. " not found")
		end
	end
end

--< player events

game.Players.PlayerAdded:connect(function(player)

local ready = false

	player.CharacterAdded:connect(function(char)

	local bp = player.Backpack
	local data = nil
	
	if ready == false then
		ready = true
		
		data = store:GetAsync(player.UserId)
		
		if data then
			setup(player, data)
			edit(player, data)
		end
	end	
	
	char.Humanoid.Died:connect(function()
		char.Humanoid:UnequipTools()
		
		local old = player.StarterGear:GetChildren()
		for i = 1, #old do
			old[i]:Destroy()
		end
		
		local new = player.Backpack:GetChildren()
		for i = 1, #new do
			new[i].Parent = player.StarterGear
		end		
	end)	
	
	--< adjuster
	
	local count = 0
	
	local function adjust()
		
	if char.Humanoid.Health > 0 then
	
		local list = {}
	
		local equipped = char:FindFirstChildOfClass("Tool")
		if equipped then
			table.insert(list, equipped.Name)
		end	
	
		local tools = bp:GetChildren()
		for i = 1, #tools do
			table.insert(list, tools[i].Name)
		end
	
		if count ~= #list then
			edit(player, list)
			count = #list
		end
	end
	end
	
	--< child events
	
	bp.ChildAdded:connect(adjust)	
	bp.ChildRemoved:connect(adjust)	
	
	char.ChildAdded:connect(function(child)
		if child.ClassName == "Tool" then
			adjust()
		end
	end)
	
	char.ChildRemoved:connect(function(child)
		if child.ClassName == "Tool" then
			adjust()
		end
	end)	
	
	end)
end)

game.Players.PlayerRemoving:connect(function(player)
	store:SetAsync(player.UserId, dir[player.Name])
	dir[player.Name] = nil
end)

--< safety

game:BindToClose(function()
	wait(5)
end)

--[[ Bssiemeow <3 ]]--

3 comments

0

I know where this script came from, buddy. Why are you copy pasting? Down voted.

0

^^^ also, why are you plagiarizing? You wrote your own name as credit... This belongs to Shiro75.

0

it doesn't so why are you harrasing me?!?

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