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 keep the sword in the inventory?

I made a system when you kill an NPC you get a sword, but when you die it disappears from the inventory

[NPC Reward]

local Enemy = script.Parent.Humanoid
local RS = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild("Event").Drop

function KillForXp() 
	local tag = Enemy:findFirstChild("creator") 
	if tag ~= nil then 
		if tag.Value ~= nil then 
			event:Fire(tag.Value)
		end 
	end 
end

Enemy.Died:connect(KillForXp) 

ServerScriptService [DropSystem]

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Event").Drop

event.Event:Connect(function(player)
	local drop = {"None", "None", "None", "None", "Sword", "Sword", "Sword", "Red Sword", "Red Sword", "Rare Sword", "None", "None", "None", "None", "None", "None",}
	local randomDrop = drop [math.random(1, #drop)]
	
	if randomDrop == "Red Sword" then
		local Redsword = script.BaconSword:Clone()
		Redsword.Parent = player.Backpack
	end
end)

[BackPackSave]

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BackpackSave")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = dataStore:GetAsync("User-"..player.UserId)
		if tool then
			for i,v in pairs (tool) do
				local toolFound = game.ServerScriptService.DropSystem:FindFirstChild(v)
				if toolFound then
					toolFound:Clone().Parent = player.Backpack
					toolFound:Clone().Parent = player.StarterGear
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		local toolsSave = {}
		for i, tool in pairs(player.Backpack:GetChildren()) do
				if tool then
					table.insert(toolsSave,tool.Name)
				end
			
			end
			dataStoreService:SetAsync("User-"..player.UserId,toolsSave)
	end)
end)

2 comments

0

I would need to see the script

0

ok

1 answer

I managed to solve it, I created another script to save the items

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