Hello! im doing an fps game and i have this issue when playing in studio the script does detect the character but when i team play or play the game instance on roblox the script doesnt detect the character wich its pretty weird
im gonna show here the part of the code that gets the character
plr.CharacterAdded:Connect(function(Char)
Character = Char or plr.Character or plr.CharacterAdded:Wait()
print(Character)
if Character then
Charadded:Fire(true,Character)
print(Char)
--LoadArms(WeaponDataFolder.VMArms:FindFirstChild("arms"),Character)
end
Character.Humanoid.Died:Connect(function()
UnloadArmsRender:Fire()
end)
end)
The character is most likely already loaded before the CharacterAdded event can listen for it. You can just check if the Character property of the player instance exists – and if it does, run the CharacterAdded function using plr.Character as the parameter:
local function OnCharacterAdded(Char)
Character = Char
if Character then
Charadded:Fire(true,Character)
end
Character.Humanoid.Died:Connect(function()
UnloadArmsRender:Fire()
end)
end
plr.CharacterAdded:Connect(OnCharacterAdded)
if (plr.Character) then
OnCharacterAdded(plr.Character)
end
Another way you can ensure Character always exists at runtime would be to define it like so:
local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait() -- If plr.Character is nil, plr.CharacterAdded:Wait() will yield until a character model is returned
local function OnCharacterAdded(Char)
Character = Char
if Character then
Charadded:Fire(true,Character)
end
Character.Humanoid.Died:Connect(function()
UnloadArmsRender:Fire()
end)
end
plr.CharacterAdded:Connect(OnCharacterAdded) -- Listens for new characters
OnCharacterAdded(Character) -- Runs the CharacterAdded function for the current character