Tool Not Activating

Hello, I ran into a problem that I managed to find the solution for. But I still require information on why this problem occurred in the first place.

I have a simple script intended for testing, I made it simple because my previous script was a combat system but I figured out nothing was wrong with the combat system itself, since this simple script also had the same issue. This is the script:

local plr = game.Players.LocalPlayer
local character = plr.CharacterAdded:Wait()

script.Parent.Activated:Connect(function()
    print("activated")
end)

In this script, it should print ‘activated’ upon clicking while the tool is equipped, but this simply doesn’t work. I found the solution was to modify the script where instead of using ‘CharacterAdded:Wait()’ it simply gets the character by ‘plr.Character’

local plr = game.Players.LocalPlayer
local character = plr.Character

script.Parent.Activated:Connect(function()
    print("activated")
end)

I made this post because I felt that using ‘CharacterAdded:Wait()’ was a much better approach to getting the character. Please let me know if this is the wrong category as this is my first post.

Hello, I 'm not a very experienced proggrammer, for me, the best option to use is plr.Character, but I think plr.CharacterAdded:Wait() works too, try if it works removing “local”.

I tested the script by removing ‘local’ but the problem still occurs. The script only works when I do ‘local character = plr.Character’ or ‘character = plr.Character’ Thanks for the help, but I am looking for a answer that explains why ‘Plr.CharacterAdded:Wait()’ doesn’t work.

The issue most likely was because plr.CharacterAdded:Wait() was yielding for the character being added, even though it was added already, which resulted in an infinite yield of the script. It works, but the issue is the function ran a little too late, so to be sure, always include plr.Character when fetching the character.

Ah, I see. I should have checked for the character using this instead.

local character = plr.Character or plr.CharacterAdded:Wait()

Thank you for the explanation!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.