I am having trouble with connecting a characteradded function, Could someone explain how to connect a characteradded function
?
So, for example
game.Players.PlayerAdded:Connect(function(plr)
Local character = plr.Character or plr.CharacterAdded:Wait()
end)
Or you can do:
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(character)
print("Character loaded!")
end)
end)
Or using a variable for doing the same.
Well you would do it like you would with any other event.
local connection
connection = player.CharacterAdded:Connect(function(character)
end)
Also :Connect
returns a connection that you can disconnect whenever you need to.
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
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 = 10redlight:Destroy()
endlocal function OnActivated()
if Debounce then
Debounce = false
Remote:FireServer()
CreateShrapnel(Character)
wait(5)
Debounce = true
end
endlocal function OnClientEvent(Player)
if Players[Player] then
CreateShrapnel(Players[Player].Character)
end
endlocal 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)
What do you mean by âmanually refreshâ??