This script's playeradded function isn't firing

local combatEvent = game.ReplicatedStorage.combatEvent

game.Players.PlayerAdded:Connect(function(plr)
	print("yo")
	local char = plr.Character or plr.CharacterAdded:Wait()
	local SwordStatus = Instance.new("StringValue", char)
	SwordStatus.Value = ""
end)

combatEvent.OnServerEvent:Connect(function(plr, eventType)
	print("fired")
	local char = plr.Character
	local ray = workspace:Raycast(char.HumanoidRootPart.Position, char.HumanoidRootPart.CFrame.LookVector * 5)
	if ray and ray.Instance then
		if ray.Instance:FindFirstChild("Humanoid") then
			local eHum = ray.Instance:FindFirstChild("Humanoid")
			if eHum.Parent.SwordStatus ~= "Parrying" then
				eHum:TakeDamage(20)
			else
				char.Humanoid:TakeDamage(5)
			end
		end
	end
end)
1 Like

Can you give us more context on which part of your code works? Does the print statement print the messages in the output?

At certain times, a game can be loaded very quickly, which causes the Players.PlayerAdded event to not be fired on time. Therefore, you should add a for loop where, before you connect the event, you perform the same code as compiled by the code inside the annonymous function of Players.PlayerAdded.

for _, plr in ipairs(Players:GetPlayers()) do
	local char = plr.Character or plr.CharacterAdded:Wait()
	local SwordStatus = Instance.new("StringValue", char)
	SwordStatus.Value = ""   
end
1 Like