Character not being detected on a roblox instance but it being detected on a studio instance

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)

Here is the footage of the local studio playing

External Media

Here is the roblox instance footage

External Media

Here is one time that the thing loaded so i guess it loads or not randomly somehow

External Media

Also i may mention the error it gave me was bc char is nil so there isnt an error more than that on output

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
2 Likes

ty so much imma check it and ill tell you

it works tysm for this ill use it more often

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