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