Why Does Script Break Here?

I want to destroy a particular object that is parented to a folder in workspace. Each model in the folder has an “IntValue” in it with the name “ID” and the value is the id number of that particular model (a player’s pet in this instance.). The script here is pretty simple. Loop through the children of the folder and find the one with the right ID and then destroy it.

So here is the code:


byepet.OnServerEvent:Connect(function(player, petid)

local ActivePets = game.Workspace:WaitForChild("ActivePets"):GetChildren()

for i, v in pairs (ActivePets) do
	print(v)
	print(v.ID.Value)
	print(petid)
	
	if v.ID.Value == petid then
	
		v.Destroy()
end		
end
end)

The prints show me that the code succeeds in getting the the petid number from the client AND it finds that v.ID.Value matches that number. But then the code breaks at the if…then… and does not reach the Destroy command.

Why??? What am I doing wrong here?

I feel like I must be missing something stupid, but I have been at this for an hour and am at a total loss.

Make an else call under the if v.ID.Value statement and get it to print something

1 Like

Okay, I added an
else
print(“no”)

and I got a “no” output. So it isn’t seeing the identity, but I don’t see why. The prints right before show that the two values are identical:

12:29:24.661 335972202 - Server - Pet Spawn Script:59
12:29:24.661 335972202 - Server - Pet Spawn Script:60

“v.Destroy()”

This is meant to be a colon, v:Destroy(). If you use a period then you’d need to write it as v.Destroy(v). Don’t think it’s the reason why your code breaks but you don’t have any other code running with that if statement so maybe you’ll want to check that out.

Good spot, I didn’t even notice that. I’m surprised the expected ":" not "." error didn’t show up. Or maybe it did, the Op just didn’t say.

Good catch. Fixed it. Still didn’t work. Apparently, there were at least two errors in this code.

Other thing that didn’t work: I tried using a variable for v.ID.Value. Nothing. I tried changing the == to = but got an error. I tried adding brackets around [v.ID.Value] but that gave me an error too …

byepet.OnServerEvent:Connect(function(player,petId) 
   local ActivePets = workspace:FindFirstChild("ActivePets") 

   for _,Pet in pairs(ActivePets:GetChildren()) do
     if (tostring(Pet.ID.Value) == tostring(petId)) then
         Pet:Destroy() 
     end
   end
end) 

Cut and pasted it into the script and got the same result.

Can you please try it again I made some mistakes.

And send me the error, if any occurs.


byepet.OnServerEvent:Connect(function(player,petId)
print(“1”)
local ActivePets = workspace:FindFirstChild(“ActivePets”)
print(“2”)
for _,Pet in pairs(ActivePets:GetChildren()) do
print(“3”)
print(Pet.ID.Value)
print(petId)
if (Pet.ID.Value == petId) then
print(“4”)
Pet:Destroy()
print(“5”)
end
end
end)

Output:

12:56:10.678 1 - Server - Pet Spawn Script:54
12:56:10.678 2 - Server - Pet Spawn Script:56
12:56:10.678 3 - Server - Pet Spawn Script:58
12:56:10.679 335972202 - Server - Pet Spawn Script:59
12:56:10.679 335972202 - Server - Pet Spawn Script:60

So the pet doesn’t get destroyed right?

Nope. Script breaks at the if … then … still. I am so totally baffled by this. What am I missing?? The folder is there. The script is picking up the right values. The values are identical. So why isn’t this working??

Can you please try newer version of the code that I’ve sent you above?

It works! Okay, what was I doing wrong? What is “tostring” and when does it need to be used?

Alright so the problem probably was because the petId that was send through remote was a string and the Pet.ID.Value was a number so you were checking if the string is same as the number and I’ve converted both of them using tostring() to strint do we are now checking if string is same as a string.

1 Like

I think your issue was that you were trying to compare a number to a string, which is not feasible. It’s the type of error that’s like comparing A to 3, which is doesn’t make sense.

You can always do print(type(Value-To-Check) to make sure that the two values you are comparing are the same type.

What he/she did right here was simply just making sure that the two values compared are both strings. If both values are actual numbers, you can also do tonumber instead of tostring.

2 Likes

Is pet.ID.Value a StringValue or IntValue? This can determine your issue.