Tool will not destroy

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)

Thanks for your time!

Can you tell us what the error prints out?

index nil with destroyyyyyyyyy

1 Like

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.

1 Like

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

1 Like

now it doesnt know what player is

1 Like

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

1 Like

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)
2 Likes

You are right sorry ! (can’t post this message if i don’t type this)

1 Like