in replicated Storage has the swords folder with a StringValue with the name of the sword.
when the player clicks the button to buy the sword. It sends to the server the Check (which loads the same name as the stringValue).
What I’m wanting to do is when the player clicks the button, the script checks if the player already has that sword in the Owned folder, if it has, it will check if it already has some stringValue in the CurrentSword folder, if it has, it destroy() and if it does not, it clones().
And if the player does not have the stringValue in the Owned folder, it take the value of sword and clones in the owned and CurrentSword folder.
The local script is working, but it is not printing the serverscript. Looks like it’s all right. But I don’t know. Could someone help?
local swords = game.ReplicatedStorage.SHOP.Swords
game.ReplicatedStorage.SHOP.Events.GearsEvent.OnServerEvent:Connect(function(player, Check, price)
if swords:FindFirstChild(Check) then
print("correct") --- is printting
local owned = player:WaitForChild("Owned")
if owned:FindFirstChild(Check) then
print("Found") -- is printting
---it's probably this part that's breaking the script. IS IT RIGHT? ---
for i, v in pairs(player.CurrentSword:GetChildren()) do
if v:IsA("StringValue") then
v:Destroy()
else
local clone = swords[Check]:Clone()
clone.Parent = player.CurrentSword
end
end
end
else
-- THIS PART IS NOT WORKING --
player.Coins.Value = player.Coins.Value - price.Value
print("Taking the money")
local clone = swords[Check]:Clone()
clone.Parent = player.Owned
local clone = swords[Check]:Clone()
clone.Parent = player.CurrentSword
end
end)
Do you have a line that :Clone()'s the script? Make the script disabled, and only enable it once you have cloned it and parented it to the CurrentSword folder.
for i, v in pairs(player.CurrentSword:GetChildren()) do
if v:IsA("StringValue") then
v:Destroy()
else
local clone = swords[Check]:Clone()
clone.Parent = player.CurrentSword
end
end
end
Is Check a string value?, if yes, then your script is destroying the check and ignoring the clone part, because it’s an else, it’ll only clone if v is not a string value, and your script will clone alot of times, because everytime v is not a StringValue, it’ll run the clone function:
and how could I do that part? Do you have any ideas? I want to take everything that has in the CurrentSword folder and destroy and then clone the item. There’s only one sword in that folder. Every time you click the button, it destroys the sword that is in the CurrentSword folder and clones a new
Use :GetDescendants() to get every children of a children, or iterate thru the whole folder and everything in it in other words, although I’m a bit confused on what part you’re asking for.
for i, v in pairs(current:GetDescendants()) do
if v:IsA("StringValue") then
print("yes") -- IS PRINTTING
v:Destroy() -- is destroying
local clone = swords[Check]:Clone()
clone.Parent = current -- is cloning
end
end
But the Else is not working
else
-- THIS PART IS NOT WORKING --
player.Coins.Value = player.Coins.Value - price.Value
print("Taking the money")
local clone = swords[Check]:Clone()
clone.Parent = player.Owned
local clone = swords[Check]:Clone()
clone.Parent = player.CurrentSword
end
for i, v in pairs(current:GetDescendants()) do
if v:IsA("StringValue") then
print("yes") -- IS PRINTTING
v:Destroy() -- is destroying
local clone = swords[Check]:Clone()
clone.Parent = current -- is cloning
else
-- THIS PART IS NOT WORKING --
player.Coins.Value = player.Coins.Value - price.Value
print("Taking the money")
local clone = swords[Check]:Clone()
clone.Parent = player.Owned
local clone = swords[Check]:Clone()
clone.Parent = player.CurrentSword
end
end