The script below is a system i was making and destroying the tool worked just fine, then it randomly started erroring and so as another error thats not related to the system im making aslong as the data key being reset, no, I dont use free models.
local Max = script.Parent.Parent
local Humanoid = script.Parent.Parent.Humanoid
local Prompt = script.Parent
local Prompt2 = script.Parent.Parent.Bag
Prompt.Triggered:Connect(function(Player)
local Juice = Player.Backpack:FindFirstChild("GrapeJuice") or Player.Character:FindFirstChild("GrapeJuice")
Max["Body Colors"].HeadColor = BrickColor.new(0.807843, 0, 0.796078)
game.SoundService.SideSFX.Pour:Play()
Juice:Destroy()
Prompt.Enabled = false
Prompt2.Enabled = true
end)
Well there’s your problem.
It cant find the juice object, so when you are calling :Destroy() on it, it throws an error.
Make sure to check if the juice object is actually in the Player Backpack or character when it is being called. If its already deleted, make sure to disconnect the prompt triggered function so it doesn’t call a non-existent object when activated again.
Here’s why the triggered event has two paramaters the prompt that the player triggered and the playerWhoTriggered as a second paramater so you should instead do
Prompt.Triggered:Connect(function(prompThatWasTrigerred,PlayerWhoTriggered)
local Juice = Player.Backpack:FindFirstChild("GrapeJuice") or Player.Character:FindFirstChild("GrapeJuice")
Max["Body Colors"].HeadColor = BrickColor.new(0.807843, 0, 0.796078)
game.SoundService.SideSFX.Pour:Play()
Juice:Destroy()
Prompt.Enabled = false
Prompt2.Enabled = true
end)
That’s why it’s saying index nil with destroy it’s because it doesn’t know what is Juice variable and it didn’t tell you it didn’t find GrapeJuice because you’re using FindFirstChild wich doesn’t output any error if it doesn’t find whatever you wrote
It does the second paramaters is the player who triggers the prompt Prompt.Triggered:Connect(function(prompThatWasTrigerred,PlayerWhoTriggered) test my code and see if it works the playerWhoTriggered is the well the player
This wont work at all, the first parameter is the player, try to use a nil check:
local Max = script.Parent.Parent
local Humanoid = script.Parent.Parent.Humanoid
local Prompt = script.Parent
local Prompt2 = script.Parent.Parent.Bag
Prompt.Triggered:Connect(function(Player)
if Player.Backpack:FindFirstChild("GrapeJuice") or Player.Character:FindFirstChild("GrapeJuice") then
local Juice = Player.Backpack:FindFirstChild("GrapeJuice") or Player.Character:FindFirstChild("GrapeJuice")
Max["Body Colors"].HeadColor = BrickColor.new(0.807843, 0, 0.796078)
game.SoundService.SideSFX.Pour:Play()
Juice:Destroy()
Prompt.Enabled = false
Prompt2.Enabled = true
else
print("GrapeJuice not found")
end
end)