OnServerEvent not working

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 fire from the server the event?

It doesn’t have to be fired from the server, the client can fire both :FireServer() and :FireClient()

That is what I mean. Does she fire it from the client to get get the onserverevent.

If she never fires the event :FireServer() then that is why it does not work

Try putting a print after local owned = player:WaitForChild("Owned"), could be a infinite yield? Try to use :FindFirstChild()

Oh, she did, she says the script prints, just until here:

as i said, local script is working fine

it wont print it, because i dont have the sword yet. theres no swords in Owned.

I cloned the string value in the owned folder and the prints are working, BUT the script dont clone to the CurrentSword folder.

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.

I believe this part is breaking the script…

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.

i changed the script to:

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

It’s probably finding the first child ‘Check’

it finds the first CHECk - printing(“correto”)

but is not printing(“Found”)

so the script should run the Else

You are ending the if before else starts, remove one end.

1 Like

Else only runs if the first conditional is declared false:

If this is false then else will run

Can you try this :

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