My sword doesn't work when cloned?

I am trying to make a sword shop for a simulator (this is my first simulator)

My sword just doesn’t seem to work when cloned

I have looked for some solutions but none of the ones I looked at had the same problem as me
I have also found that the activated function doesn’t run for some reason after cloned and I checked that it wasn’t the tool itself because it works if I put it under starter player

my sword script:

local canAttack = true
local sword = script.Parent
local canDamage = false

function onEquip()
	local Character = script.Parent.Parent
	local player = game.Players:GetPlayerFromCharacter(Character)
	local function Attack()
	local attack = sword.Parent.Humanoid:LoadAnimation(script.SwordSwing)
	
	
	if canAttack == true then
		attack:Play()
		player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1
		canAttack = false
		
		canDamage = true
		wait (0.5)
		canDamage = false
		wait (0.25)
		canAttack = true
	end
	
end
sword.Activated:Connect(Attack)
end
sword.Equipped:Connect(onEquip)

function onTouch(part)
	local character = part.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	
	if player then
		if canDamage == true then 
			character.Humanoid:TakeDamage(20)
			
		end
	end
	
end
sword.Blade.Touched:Connect(onTouch)

function Attack()
	local attack = sword.Parent.Humanoid:LoadAnimation(script.SwordSwing)
	
	
	if canAttack == true then
		attack:Play()
		canAttack = false
		
		canDamage = true
		wait (0.5)
		canDamage = false
		wait (0.25)
		canAttack = true
	end
	
end
sword.Activated:Connect(Attack)

Is the script in the sword when you are cloning it…? And from where are you cloning it.

yes the script is in the sword and I am cloning it from the replicated storage.

Try disabling the script, and when you have cloned it inside of the players backpack. Enable it.

Thanks for responding, I really appreciate it.

1 Like

No problem! I’m always happy to help.

I have. This is my clone script

local players = game:GetService("Players")
local h = game:GetService("ReplicatedStorage")
local g = game:GetService("ServerStorage")

local check = h.Check
local purcahse = h.Purchase

purcahse.OnServerEvent:Connect(function(player, weapon, weaponName)
	if weapon == weaponName then
		local leaderstats = player:FindFirstChild("leaderstats")
		local cash = leaderstats:FindFirstChild("Cash")
		if cash then
			cash.Value = cash.Value - weapon.PurchaseCash
			h:FindFirstChild(weaponName):Clone().Parent = player.Backpack
		end
	end
end)
local players = game:GetService("Players")
local h = game:GetService("ReplicatedStorage")
local g = game:GetService("ServerStorage")

local check = h.Check
local purchaseEvent = h.Purchase

purchaseEvent.OnServerEvent:Connect(function(player, weapon, weaponName)
	if weapon == weaponName then
		local leaderstats = player:FindFirstChild("leaderstats")
		local cash = leaderstats:FindFirstChild("Cash")
		if cash then
			if cash.Value >= weapon.PurchaseCash then -- checking if the player has enough cash to purchase this item.
				if not player.Backpack:FindFirstChild(h[weaponName]) then -- if the player doesn't have the item
					cash.Value = cash.Value - weapon.PurchaseCash
					local clonedWeapon = h:FindFirstChild(weaponName):Clone()
					clonedWeapon.Script.Disabled = false
				end
			end
		end
	end
end)

try this

still doesn’t work. Thank you for all of your efforts so far! I wish that could be the answer but it isn’t.

Wait, I messed up so bad lol. 1 second please.

1 Like
local players = game:GetService("Players")
local h = game:GetService("ReplicatedStorage")
local g = game:GetService("ServerStorage")

local purchaseEvent = h.Purchase

purchaseEvent.OnServerEvent:Connect(function(player, weapon, weaponName)
	if weapon == weaponName then
		local leaderstats = player:FindFirstChild("leaderstats")
		local cash = leaderstats:FindFirstChild("Cash")
		if cash then
			if cash.Value >= weapon.PurchaseCash then -- checking if the player has enough cash to purchase this item.
				if not player.Backpack:FindFirstChild(weaponName) then -- if the player doesn't have the item
					cash.Value -= weapon.PurchaseCash
					local clonedWeapon = g:FindFirstChild(weaponName):Clone()
					clonedWeapon.Parent = player.Backpack
					clonedWeapon.Script.Disabled = false
				end
			end
		end
	end
end)

also put the tool in ServerStorage so exploiters can’t steal it.

I think I’m still going to use replicated storage just for now. I’m testing right now.

Okay just change the script because I put serverstorage

this wont work because the script thinks that the character is serverstorage i highly recommend disabling it and after cloning it enable it again

its what I did but it still doesn’t seem to work. For some reason it doesn’t enable again

Ah I think I see the problem but I don’t know how to fix it. The problem is that for some reason the weapon is getting cloned locally instead of globally.