Getting only part of a cloned instance's name

So, Ive been working on this script, that loops trough all the tools in the player’s backpack, copies the tools and then moves them to the player when he dies, the issue im having here is that my script changes the name of the part when it is cloned, for example, if the player had a sword in his inventory, it would clone it, rename it “Stolen sword” and move it to the player position when he dies, it works perfectly… But the issue here is that all it does is add “Stolen” to the name of the item, wich means if a player is carrying a item like a “Stolen sword” and dies with it, the sword will be renamed to “Stolen Stolen Sword”.

Does anybody know how i can fix this, is there something that i could use to check if the item already has “Stolen” in its name

Here’s the code, sorry if its messy

local char = script.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local hum = char:FindFirstChild("Humanoid")
local event = game.ReplicatedStorage.PlayerDied



hum.Died:Connect(function()
	local Cfra = char.HumanoidRootPart.CFrame
		event:FireClient(plr)
	pack = plr.Backpack:GetChildren()
	print("the player has "..#pack.." items in their inventory")
		for i, v in pairs(pack) do
			itemDropped = false
			if itemDropped == true then
				print("Item was already dropped, lets not duplicate")
			elseif itemDropped == false then
				itemDropped = true
				print("item wasn't dropped, drop it")
			local klon = v:Clone()
		klon.Parent = workspace
		klon.Name = "Stolen "..v.Name
		klon.Handle.CFrame = Cfra + Vector3.new(0,10,0)
		end
	end
	end)
1 Like

Can check if the start of the tools name is equal to “Stolen” and if it is dont change the name.

You can do this with string.sub:
string (roblox.com)

1 Like
local char = script.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local hum = char:FindFirstChild("Humanoid")
local event = game.ReplicatedStorage.PlayerDied



hum.Died:Connect(function()
	local Cfra = char.HumanoidRootPart.CFrame
	event:FireClient(plr)
	pack = plr.Backpack:GetChildren()
	for i, v in pairs(pack) do
		itemDropped = false
		if itemDropped == false then
			itemDropped = true
			local klon = v:Clone()
			klon.Parent = workspace
			if not string.sub(klon.Name,1,7)=="Stolen "then
				klon.Name = "Stolen "..v.Name
			end
			klon.Handle.CFrame = Cfra + Vector3.new(0,10,0)
		end
	end
end)

that should probably work
afbeelding

Does not work, the name just stays “Sword” now, but ill mess around with your fix to see if i can get it working

make sure you replaced
afbeelding
with
afbeelding

1 Like

I did, the script just does not detect it for some reason

Nevermind im just dumb, i found a error in the code above.