How do I connect the Characteradded:Wait() function?

No like in the end of the script for example:

local tool = script.Parent
Tool.Activated:Connect(function()
print(“die”)
end
Tool.Activated:connect(OnActivated)

Can you please explain what the Event is supposed to do at the end of the script please?

Event connection, Events | Documentation - Roblox Creator Hub at the “Connecting a function” part.

I’m having trouble with connecting my characteradded function…

function onCharacterAdded()
 print("Char added!")
-- Anything else you want to execute when the character is added
end
    
Player.CharacterAdded:Connect(onCharacterAdded)

Is this what you are trying to do?

I dont know why it doesnt work, I got a

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

In my script and it still doesnt work…

Also its a local script in Starterplayerscripts

The code which you are using waits for the Character to load if it hasn’t already.

Can I know what you are trying to do, specifically?

My tool works but only once, Once you use it or die you cannot use it again. I’ve been told its the CharacterAdded:wait() thing that makes the tool work only once.

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

while not char do or not char.Parent do
  char = plr.Character
  wait()
end

The bug you are facing occurs because when Player.Character property is a reference to the player’s old character which has been destroyed and therefore has parent nil . You might want to use something like the above code to access the Character
@vovcher

Errors if i remove the characteradded:wait() part

20:01:36.070 - Players.vovcher.PlayerScripts.ShrapnelLauncher:20: attempt to index local ‘Target’ (a nil value)

you might have used different variable for player or character in your script, can you check if that’s not the case?

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(CharacterAddedEvent)
end)

function CharacterAddedEvent(character)
    print("Added "..character.Name);
end

A few remarks of this event.

Whenever a player character is loaded into the world, a CharacterAdded event signal is fired. It also passes the character object that has been loaded as an argument.

By connecting our CharacterAddedEvent(character) function to the event. We are subscribing to it, which means whenever the signal is fired, the connected function(s) will get called. Matching parameters will be assigned.

These should be handled inside a ServerScript on the server. You have to take into consideration that players may not load in time for the StarterPlayerScripts or any LocalScript to be able to catch the event when it actually occurs.

For clarification, your LocalScript may load after the CharacterAdded event has already been fired. Since client to server delay is possible and connection speeds can impact how quick a LocalScript can load.

For the CharacterAddded:Wait() function call, it returns a character object.

You can use it in a ServerScript or LocalScript as follows:
[Player object here].CharacterAdded:Wait();

You must be calling it on a player object for it to work. In a LocalScript, you can use the following:

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

But i already have a server scripts which replicates the effects to other players since the script is local,

Also i forgot to post the script but here:

local Workspace = game:GetService(“Workspace”)
local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

local Player = Players.LocalPlayer
local Backpack = Player:WaitForChild(“Backpack”)
local Remote = ReplicatedStorage:WaitForChild(“Remote”)

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Tool = Backpack:WaitForChild(“Tool”)
local Handle = Tool:WaitForChild(“Handle”)
local Animation = Tool:WaitForChild(“Animation”)
local BombBeepingSound = Handle:WaitForChild(“BombBeeping”)

local Debounce = true

local function CreateShrapnel(Target)
local Humanoid = Target:WaitForChild(“Humanoid”)
print(“Creating Shrapnel”)
local AnimationTrack = Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
BombBeepingSound:Play()

local redlight = Instance.new(“SpotLight”)
redlight.Color = Color3.fromRGB(255,0,0)
redlight.Parent = Target.HumanoidRootPart
redlight.Brightness = 100
redlight.Range = 100
redlight.Face = “Bottom”

 wait(2)

local ex = Instance.new(“Explosion”)
ex.Position = Target.HumanoidRootPart.Position
ex.Parent = workspace
ex.BlastRadius = 10

redlight:Destroy()
end

local function OnActivated()
if Debounce then
Debounce = false
Remote:FireServer()
CreateShrapnel(Character)
wait(5)
Debounce = true
end
end

local function OnClientEvent(Player)
if Players[Player] then
CreateShrapnel(Players[Player].Character)
end
end

local function CharacterAddedEvent(character)
print("Added "…character.Name);
end
Tool.Activated:Connect(OnActivated)
Remote.OnClientEvent:Connect(OnClientEvent)
Player.CharacterAddedEvent:Connect(Character)

This is why it’s better to ask directly about your problem instead of a roundabout question that’s completely unrelated to your problem. CharacterAdded is a signal under the player which fires when a new character is defined for the player. You can either connect a function that runs when this fires, or use Wait on the signal to yield the current thread until the event is fired, which then the arguments will be returned.

If your issue is that you have a non-resetting LocalScript which is supposedly not working upon the next respawn, that is because you are referencing the old character. You are required to manually refresh this variable.

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

player.CharacterAdded:Connect(function (newChar)
    character = newChar
end)
3 Likes

What do you mean by “manually refresh”??

With manually refreshed he meant, when the Character Dies and is reloaded you need to declare the new Character model to the variable or the script refers to the Old one which isn’t accessible anymore. @vovcher

player.CharacterAdded:Connect(function (newChar)
    character = newChar
end)

the above code does that for you

Is this what you are looking for?

It still doesnt work even after i try everything