Trouble with tools

I am having a couple issues with tools I’m trying to make. The first issue is with one specific tool that I want to disappear after the player uses it a few times. I was able to achieve this with other tools using the same script, but it won’t work with this one. The second issue is that I’m trying to animate it, but it won’t work. Here is the script:

local tool = script.Parent
local clicks = 0

tool.Activated:Connect(function(player)
	local character = player.Character
	if not character or not character.Parent then
		character = player.CharacterAdded:Wait()
	end
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	local drinkDrink = Instance.new("Animation")
	drinkDrink.AnimationId = "rbxassetid://9263553535"
	local drinkDrinkTrack = animator:LoadAnimation(drinkDrink)

	drinkDrinkTrack:Play()
	drinkDrinkTrack.Stopped:Wait()

	clicks = clicks + 1

	if clicks >= 5 then
		tool:Destroy()
	end
end)

I’m not getting any errors to tell me what’s causing the first issue, but for the second issue, it says it is attempting to index nil with character.

To begin really easy solution that some people forget about with tools: Is there a handle

As @Inf_idel said, if there isn’t a handle then find the main part of your tool and name it Handle with the capital H.

Very, very good point:

  1. Make sure there is a “Handle”
  2. local player = script.Parent.Parent
1 Like

To get the player you can use this;
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
Just add an if statement like

if player then
 -- do stuff
end
1 Like

Its because Tool.Activated does NOT return the player

How do we fix? We need to get the player from the character, so lets do this

local Player
local Character

local Tool = script.Parent

Tool.Equipped:Connect(function(Mouse)) --although we aren't going to use the mouse
     if Character == nil and Player == nil then --we check if Character and Player havent been defined yet
          Player = game.Players:GetPlayerFromCharacter(Tool.Parent)
          Character = Player.Character
     end
end)

This little piece of code will solve your problem

2 Likes

Yeah it should, but it’s weird, even after testing a tool in Studio it wasn’t working. Maybe something’s off, idk.

Yes. There is a handle in the tool.

Thank you all for your suggestions, but none of them have worked. Any other ideas?

alright i think i’ve got you

i think it’s the animation fault for being broken

first change the animation priority to be something such as action
image
oh yeah I think you gotta export it again as the same animation

and if you need help just ask
also here’s the tool i’ve made, might not work with my animation
toolanimation.rbxm (3.7 KB)

1 Like