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