Error while Destroying an object/part/model sent by Remote events

Since I Destroy Food (while eating) from the Server, the Destroy method doesn’t work anymore, it throws me an error:
Error while firing a new event (event 7) from Leolol_DB Custom error
ServerScriptService.EventService:32: attempt to call method ‘Destroy’ (a nil value) The error
Her’s the line that doesn’t work:

Data[2]:Destroy()

Data[2] is the object to destroy (because it was eaten)
The line that send the Data

NewEvent:FireServer(7,{Nut[ObjNum],Object})
--NewEvent is the Event's Name, 7 is for the eating event
--Nut = {48,32,24,16} , ObjNum is the Food's number (1 to 4), Object is the Food (Model) to destroy :)

The event (NewEvent) is firing correctly (the Server is adding the food’s nutrition to the food bar but when Data[2]:Destroy() is reached, it throw an error :frowning:
If you need more informations to resolve this,: Reply!

1 Like

I’m not sure how to solve this but you could try simply moving the tool out of the player’s inventory, then destroying it somewhere else.

Humm… You didn’t understand…
My script is almost trying to do that:

game.Workspace.Food:Destroy() --Food is what object that is eaten

I’ts the normal :Destroy() method

Oh wow okay yeah sorry I read that completely wrong. Have a go at using remove rather than destroy. Instance | Documentation - Roblox Creator Hub

It’s deprecated :slight_smile:

1 Like

I should read more closely wow

Buffet%20in%20AMRoom
It’s a picture of the update of Becants 0.0.9
Before, the Script with Destroy method was in each Food-things…
Now, it’s only ONE LINE of a script that do the same thing :slight_smile:
But this line don’t work :frowning:

What have you changed which broke the script?

It looks like Data[2] isn’t an Instance. Please print(typeof(Data[2])) and tell us what is outputted.

It output the Food-Model’s Name that will be eaten
So… It’s a Instance (Model)

So, placing print(typeof(Data[2])) directly before the throwing Destroy call outputs ‘Instance’? I need you to verify this by actually doing it and telling me the result.

Ok… :slightly_smiling_face:

It outputs:
MeatyThing (it’s the pink food that I selected)
Error while firing a new event (event 7) from Leolol_DB
ServerScriptService.EventService:32: attempt to call method ‘Destroy’ (a nil value)

Oh! I forgot typeof!

String WUT???
Error while firing a new event (event 7) from Leolol_DB
ServerScriptService.EventService:32: attempt to call method ‘Destroy’ (a nil value)

The issue is that Data[2] is a string, not an Instance. You’re trying to call the Destroy method of a string, when no such method exists. Make sure you’re sending the instance itself to the server, and not the instance’s name.

Data[2] is a string but the script is setting Data[2] to a Model! (Or Instance)

The code will still throw an error if the field is set to an instance after you attempt to call Destroy on a string.

It’s before, when sending Data :neutral_face:

If the client changes the string to an instance after sending the table to the server, the server will still receive the copy of the original table with the string. I don’t know what’s happening to cause this, but at the time of destroying the element, the element is a string.

It’s sending a Instance but the Server catch a string :neutral_face:

In fact, in 0.0.9, there was a Script for each Food-Thing, but now (0.0.26) I deleted theses Script to change into one single Script (that use 3 lines of codes), and it’s lighter now :slight_smile:

1 Like

Try reverting to this individual script system and try the process again