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)
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)
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.
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.