Tool is completely broken when cloned, yet works fine in StarterPack?

I am working on this murder mystery styled game, and my knife tool is just completely broken whenever it’s cloned to a player. I have no idea why this is happening, and yes I’m cloning it from a server script. There’s no error in the developer console either.

I’ll share my code and some videos of the error.


The knife, when being used in StarterPack


The knife, when being cloned from the server.


-- Code removed for security purposes

Maybe it’s because of this line in the LocalScript:

local Character = Player.CharacterAdded:Wait() or Player.Character

The script is waiting for the player’s character to be added, but it’s already added.

Whenever you attempt to store the player’s character in a variable, you should reference it like this:

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

If you define a variable with an or, the second value will only be used if the first value is nil. And in this case, Player.CharacterAdded:Wait() won’t be nil, but instead yield since it’s waiting for a new character; and only after the Player.CharacteredAdded event fires and it returns nil – which isn’t possible – will player.Character be used.

If you put player.Character first, it may be nil, and if so the script will use the second value (player.CharacterAdded:Wait()). This will save the script from unnecessarily yielding until a new character is added.

I’ll try it and see if it works.

It worked! Thank you so much! :grin:

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